Skip to main content

Reading Text Files


Introduction:
This page will teach you about reading from text files in Java.

What is used for reading from text files?
There are many ways to read from text files in Java but the way I will talk about is using BufferedReader with FileReaders. Because of this we need to import two namespaces as well as two extra namespaces used for separate exceptions;

  1. import java.io.BufferedReader;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.IOException;

The Structure:
To read from a file we first need the file path of the text file as a string, we need the four imports listed above, and you will also need knowledge of Try and Catch statements, as well as Do While statements.

Examples:
The first thing we need to do is create a simple Try and Catch statement:

  1. try {
  2. } catch (FileNotFoundException e1) {
  3. e1.printStackTrace();
  4. }

The statement throws a FileNotFoundException (which pretty simple and self-explanatory) but it handles the error if the file is not found that is trying to be read by the program. Now we need to create a BufferedReader from a FileReader of the text file path (this will essentially just grab the file for use)...

  1. try {
  2. BufferedReader br = new BufferedReader(new FileReader("C:/Users/Josh/Desktop/read.txt"));
  3. } catch (FileNotFoundException e1) {
  4. e1.printStackTrace();
  5. }

Next we want to create a new String variable and initiate it as a blank String (to avoid an error) and then we want to read each line of the BufferedReader text file and add each line to the "line" string variable. This will be done through a Do While statement...

This is also encased in a Try and Catch statement (IO Exception) just in case there is a problem with the file (a line is blank etc). Here is the full source...

  1. try {
  2. BufferedReader br = new BufferedReader(new FileReader("C:/Users/Josh/Desktop/read.txt"));
  3. String line = "";
  4. try {
  5. do {
  6. line += br.readLine();
  7. }while (br.readLine() != null);
  8. } catch (IOException e) {
  9. e.printStackTrace();
  10. }
  11. System.out.println(line);
  12. } catch (FileNotFoundException e1) {
  13. e1.printStackTrace();
  14. }

Which gives the output which is contained in our "read.txt" file:

This is our read text file.

Finished!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.