How To Read a File in Java

There are various ways of writing a text file and reading it in Java. This is necessary when dealing with different types of applications.

For example, a plain text file in Java can be read by using any of the methods given below.

  1. FileReader
  2. BufferedReader 
  3. Scanner

Each utility mentioned above offers something unique, e.g. BufferedReader offers information buffering for fast reading, and Scanner offers the ability to parse.

A text file in Java can also be read by using both the BufferedReader and the Scanner utility. 

For reading different types of files in unique situations, you can choose and use either of these methods. 

Reading a Text File in Java Using BufferedReader

This method takes the file input as a character stream and it works best if you want to read a file line-by-line. The default buffer size is 8KB but it can be changed depending on the situation, but most of the time the default size is enough for performing the operations. BufferReader is a synchronized method for reading files, and it can safely perform operations using multiple threads. This method also reads large files efficiently. 

Attached below is an example of the code to help you understand the use of BufferedReader more clearly.

BufferedReader

Here, first, create a ‘try to catch’ block to handle the exceptions, if any. After that, create a 000000file object which will contain the filename for which the content has to be read. 

The name of the file is provided by the getAbsoluteFile method. After that, create a BufferedReader object which is linked to the earlier FileReader object, which will fetch the file content.

Full Stack Java Developer Course

In Partnership with HIRIST and HackerEarthEXPLORE COURSE
Full Stack Java Developer Course

Reading a Text File in Java Using FileReader

The FileReader class in Java is a character-oriented class that is also used in File Handling. 

Two types of constructors can be used in the FileReader class:

  • FileReader(String File) 

It gets the filename as a string instance. This is used to open the given file in “read mode”. If no file exists by the mentioned name, then it throws FileNotFoundException. 

  • FileReader(File File)

This is the same as the above constructor, except that it gets the filename in “file instance”.

Below is an example that shows the usage of FileReader class.

FileReader

The code above has used the FileReader(File File) constructor and it will throw the exception FileNotFoundException, if there is no file by the name of “file.txt”. 

int read() is used to return a character in its ASCII form and it returns -1 when the file reaches its end.

FREE Java Certification Training

Learn A-Z of Java like never beforeEnroll Now
FREE Java Certification Training

Reading a Text File in Java Using Scanner

The scanner is a class in Java in java.util package that is used to parse primitive data types and strings using regular expressions. Using this class, you can read any text from an object that uses the Readable interface.

To read a text file using the Scanner class, create a Scanner that generates the scanned values from the specified file. An example is shown below using the try-with-resources block that automatically takes control of scanner.close() .

Scanner

Get a firm foundation in Java, the most commonly used programming language in software development with the Java Certification Training Course.

Conclusion

With this tutorial, you would have gained a working understanding of how to read a file in Java. If you are looking to build a career in Software Development, Java is a vital programming language that you have to have as part of your skillset. 

Furthermore, a comprehensive course that teaches you all that you need to become a Java expert will come in handy. You can explore this Java Certification Training Course. This 60-hour applied learning program will give you a firm foundation in Java and it provides plenty of practice, including 35 coding exercises to be performed on Core Java 8.

If you are looking for more elaborate learning that will help you master Full-Stack web development with a focus on Java, please check out Simplilearn’s Full Stack Java Developer Master’s program, in partnership with HIRIST and HackerEarth. This Bootcamp program will help you master over 30+ in-demand Full Stack Java Development skills and tools with practical training. It includes 20 lesson-ends, 6 phase-end projects, and 4 industry-aligned capstone projects.

We hope you enjoyed this tutorial and are looking forward to exploring other aspects of Java and Software Development. If you have any questions on this “How to read a file in Java” article, feel free to share them in the comments section of the article. Our subject-matter experts will review and respond to them at the earliest.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.