I am using Java's MappedByteBuffer with a fixed buffer size to write into a file, as part of an assignment. It should write character by character since the buffer size can be smaller than the line length. But the problem is that it writes the remaining positions of the buffer at the end of the file as null characters. How can I remove those null characters?
Here's an example code:
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class Main {
public static void main(String[] args) throws IOException {
int bufferSize = 20;
RandomAccessFile file = new RandomAccessFile("test.txt", "rw");
MappedByteBuffer buffer = file.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, bufferSize);
for (char c : "Line1".toCharArray()) {
buffer.put((byte) c);
}
buffer.put((byte)'\n');
for (char c : "Line2".toCharArray()) {
buffer.put((byte) c);
}
}
}
And here's the output (opened with Sublime Text utf-8 encoding):