readLine()
returns a String, but what I need are the ASCII
characters of the characters of each string on each line.
E.g.
ABCDE XYZ
I need the ASCII characters of A, B, C, D, E, and also X, Y, Z, but separately. Currently, I'm using BufferedReader
's readLine
method, then loop through the length of the string and using String.charAt()
to retrieve the character at each position. It's then trivial to convert characters to bytes.
I figured it must be expensive to create strings from bytes, then convert back into bytes. I tried writing my own solution using FileChannel
:
FileChannel f = FileChannel.open(Paths.get("file.txt"));
ByteBuffer buffer = ByteBuffer.allocate(10000);
bytesRead = 0;
byte[] chunk = new byte[10000];
while (bytesRead != -1) {
bytesRead = f.read(buffer);
buffer.flip();
while (buffer.hasRemaining()) {
buffer.get(chunk, 0, bytesRead);
processChunk(chunk);
}
buffer.clear();
}
I don't know why but this turned out to be slower than BufferedReader
's readLine()
method.
I found this answer: Why is the performance of BufferedReader so much worse than BufferedInputStream? and I was wondering if BufferedInputStream
could be of help to me.
Speed is of utmost priority for this!