I had an interview and they asked me to write a java code to read a file from a DISK. I know that I can use both FileInputStream
and BufferedReaders
.
But what is the most suitable and what is the reason ? Is there something special when we read from a disk?
2 Answers
The docs state one usecase for FileInputStream
:
FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.
So for Readers, the opposite apply.
A FileInputStream
is reading byte
by byte
, while the BufferedReader
is reading char
by char
.
So if you're reading something with chars, use a Reader. If you're reading binary data, use a Stream.

- 6,440
- 1
- 21
- 38
Using a Scanner is a good option for reading files. You can construct a Scanner object using a File, an InputStream or a Path object. In addition, a Scanner provides you with several built-in functions which can read most primitive types directly from the source.
Here is an example code for the usage of a Scanner to read long values from a file:
Scanner sc = new Scanner(new File("myNumbers.txt"));
while (sc.hasNextLong()) {
long aLong = sc.nextLong();
}
The discussion here may be a helpful resource for you: Reading a plain text file in Java
I also find this resource helpful for my studies: Ways of reading text file in Java

- 24
- 1
- 4
-
Unfortunately, this is not related to this is question in any way :/ – maio290 Jan 17 '19 at 06:29
-
Why do you suggest it is not related? I wanted to propose an alternative way to streams and readers. Scanner class is very simple and useful way while you are reading a file. Actually when I first come across to Scanner class was again an answer to a question which discusses barely InputStreams. So, I don't think that it is unrelated to topic. – kader Jan 17 '19 at 06:34