Tell me how to write several ByteBuffer correctly in one txt file. I write one buffer to a separate file like 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 to write several byteBuffer in one txt file in sequence?

    1 answer 1

    You can record by first specifying the size of byteBuffer, then byteBuffer itself. When reading accordingly read similarly.

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

    When reading, you will need to read the position for reading (Something like this is not a code, but approximately how to read)

     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;