有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java类似于二进制数据的扫描器

有类似于二进制数据扫描仪的东西吗?例如,我希望在每次调用某个方法时使用类似“\r\n”的分隔符并获取byte[]。你能为我提供能创造奇迹的课程吗


共 (3) 个答案

  1. # 1 楼答案

    您可以使用java.io.InputStream类:

    InputStream is = new FileInputStream("your_file");
    byte[] b = new byte[is.available()];  //reads how much bytes are readable from file
    is.read(b);//reads the file and save all read bytes into b
    

    希望这有帮助

  2. # 2 楼答案

    雅加达公共图书馆有一个你可以使用的图书馆。只需下载。从jakarta下载他们的jar。jar并通过添加外部jar将其添加到构建路径中IOUtils.toByteArray(InputStream input)

  3. # 3 楼答案

    考虑使用{a1}和^{}

    File file = new File("binaryFile.dat");
    FileOutputStream fos = new FileOutputStream(file);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    DataOutputStream dos = new DataOutputStream(bos);
    
    byte[] array1 = ...
    byte[] array2 = ...
    byte[] array3 = ...
    
    dos.writeInt(array1.length);
    for(byte b : array1) dos.wrtieByte(b);
    
    dos.writeInt(array2.length);
    for(byte b : array2) dos.wrtieByte(b);
    
    dos.writeInt(array3.length);
    for(byte b : array3) dos.wrtieByte(b);
    

    然后像这样读:

    File file = new File("binaryFile.dat");
    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fis);
    DataInputStream dis = new DataInputStream(bis);
    
    byte[] array1 = new byte[dis.readInt()];
    for(int i = 0; i < array1.length; i++) array1[i] = dis.readByte();
    
    byte[] array2 = new byte[dis.readInt()];
    for(int i = 0; i < array2.length; i++) array2[i] = dis.readByte();
    
    byte[] array3 = new byte[dis.readInt()];
    for(int i = 0; i < array3.length; i++) array3[i] = dis.readByte();