So I know of two methods of reading or writing files in java.

The first is the read(byte[]) method provided by InputStream class.

The second is the readLine or write() method provide by the Buffered prototype.

Which is the more efficient one out of these? Which one would you recommend?

Which you use depends on whether you are processing raw bytes of data or lines of text.
Efficiency won't be a deciding issue in any case, provided that use have Buffered stream of some kind.

Edited 5 Years Ago by JamesCherrill: n/a

Which you use depends on whether you are processing raw bytes of data or lines of text.
Efficiency won't be a deciding issue in any case, provided that use have Buffered stream of some kind.

Would it make any difference if I read a text file in byte mode rather than using BufferedReader?

If your text is just US ASCII (chars < 127), not much difference. If you are using any text from anywhere else in the world it makes a big different because Java works in 16 bit international UniCode, and handles text properly in just about every language known to man.

Another doubt just popped up in my mind.

This is what I use to read a file.
BufferedReader reader = new BufferedReader( new FileReader(fileName) );

However, is it also possible to the above operation this way:
BufferedReader reader = new BufferedReader( new InputStreamReader(fileName) );

FileReader is just a subclass of InputStreamReader, with defaults for the settings you have to handle explicitly in InputStreamReader - so unless you want to do anything special it's just the same this but a bit more convenient. Check out the API doc for FileReader for full details.

This article has been dead for over six months. Start a new discussion instead.