How right to record a few byteBuffer in one file.



  • Tell me how to record a few ByteBuffer in one txt file properly. In a separate file, I write this:

    File file;
    File path = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS);
    file = new File(path, "first.txt");
    boolean append = true;
    try {
        FileChannel wChannel = new FileOutputStream(file, append).getChannel();
        wChannel.write(byteBuffer);
        wChannel.close();
    } catch (IOException e) {
        e.printStackTrace();
    }        
    

    How's one txt file consistently record a few byteBuffer?



  • You can record the size byteBuffer first, then byteBuffer. Read the same for reading.

    wChannel.write(byteBuffer.length);
    wChannel.write(byteBuffer);
    wChannel.write(byteBuffer2.length);
    wChannel.write(byteBuffer2);
    

    On reading, it's gonna take a reading position.

    int index = 0;
    int len = byteBuffer[index];
    index++;
    newBytes = wChannel.read(byteBuffer,index,len);
    index+=len;
    len = byteBuffer[index];
    index++;
    newBytes2 = wChannel.read(byteBuffer,index,len);
    index+=len;
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2