Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BufferedReader vs. RandomAccessFile in java

I'm writing a small application in java

I read text files in various sizes and I need to read them line by line (and insert the line into array).
Is there difference between BufferedReader.ReadLine() and RandomAccessFile.ReadLine(), in terms of performance?

Is there any reason to prefer one or the other?

like image 883
choppy Avatar asked May 01 '12 16:05

choppy


1 Answers

RandomAccessFile.readLine() might be slightly faster because it ignores character encoding. However it doesn't use buffering and still use StringBuffer :P so it could be slower on your system.

BufferedReader.readLine() is preferred because it handles character encoding e.g. UTF-8 or Windows-1252.

There is also a DataInputStream.readLine() which can be used with BufferedInputStream. Only use this is you can be sure you want ISO-8859-1 or ASCII encoding.

like image 73
Peter Lawrey Avatar answered Oct 01 '22 05:10

Peter Lawrey