How to read a file in Java

In this post we will demonstrate how to read a file in java using the Files, BufferedReader and FileInputStream classes.¬†The examples below will read a file called “testFile.txt” which has the following content:

How to read a file using Files:

The example below uses the Files class which is one of the most simplest solution on reading a file.

How to read a file in Java using BufferedReader:

The following examples uses a BufferedReader with a FileReader.

How to read a file in Java using FileInputStream:

The last example we want to show reading a file using the FileInputStream with Apache Commons IOUtils


Below is the output from running any of the above code samples.

In summary we have illustrated three different ways on how to read a file in java. Below I have provided links to the API documentation, of the classes used in this post.


  1. Java Files API documentation
  2. Java FilesReader API documentation
  3. Java BufferedReader API documentation
  4. Java FileInputStream API documentation

Similar posts:

  1. How to create a file in Java
  2. How to parse a JSON file using GSON


Leave a Reply