How to Read File in Java

In many Java application there is a requirement to read or write a file. In Java there are various ways to read or write a file, in this post we’ll see some of the ways you can read a file in Java.

Some of the options you have for reading a file in Java are-

  1. Using BufferedReader wrapped around a Reader. See example.
  2. Using Scanner class. See example.
  3. Using methods in java.nio.file.Files class like readAllLines(), newBufferedReader along with lines() method. See example.

To see different options available in Java for writing a file refer this post- How to Write to a File in Java

Using BufferedReader to read file in Java

Using BufferedReader you can read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders.

Java program to read file using BufferedReader in Java

Using try-with-resources you can shorten the code you need to write as you won’t be needing finally block in that case to explicitly close the stream. Stream will be closed automatically.

BufferedReader with try-with-resources

Using Scanner to read file in Java

You can also use Scanner class to read file in Java. Scanner class has a constructor where you can pass a File as parameter. Using method nextLine() you can read file line by line where each line is returned as String.

Java program to read file using Scanner

Using Files.readAllLines method

Java 8 onward you can use readAllLines() method of Files class to read a file in Java.

As you can see readAllLines() method reads the whole file at once and store it into a List which is not a very efficient way of reading a file. Files class provides a better way to read a file using newBufferedReader method.

Using Files.newBufferedReader method to read file

newBufferedReader method opens a file for reading, returning a BufferedReader to read text from the file in an efficient manner.
Once you have the BufferedReader instance you can use lines() method to read the file line by line. This method returns a Stream, the elements of which are lines read from this BufferedReader. The Stream is lazily populated, i.e., read only occurs during the terminal stream operation.

Java program to read file using Files.newBufferedReader method

Using Files.lines method to read file

Files class also has lines() method that you can use to read a file in Java.

That’s all for the topic How to Read File in Java. If something is missing or you have something to share about the topic please write a comment.

You may also like

One Comment

  1. UshaK, thank you for your blog post.Really thank you! Awesome.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.