choppy
choppy

Reputation: 749

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?

Upvotes: 8

Views: 8472

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533492

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.

Upvotes: 6

Related Questions