Reading from a character based file using the FileReader

One of the common problems we always hit is the File IO. This article quickly takes you through the very simple steps involved in how to read from a character based file (text file) and display its contents using your Java code with a working example that you can download.

This article focuses on how to read from a character based file rather than binary files.

FileReader:

The Java SDK provided a very handy utility class called the FileReader (java.io.FileReader) to quickly access character based files. In the most simple case, all you need to is to instantiate the FileReader with a StringThe String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Below are all the articles related to String. which is the path to the file that you want to read. It is as simple as

FileReader fileReader = new FileReader(filePath); // throws FileNotFoundException

Note that instantiating a FileReader using a file path will throw a FileNotFoundException exception if the file itself does not exist, or is a directory/folder , or any other problems that might prevent the file from being accessed/read.

BufferedReader:

Once the FileReader is created with the file path, we are now ready to read the contents of the file. To do this we can use the BufferedReader (java.io.BufferedReader) class. A BufferedReader can be used a wrapper around the FileReader to read line by line. Without the BufferedReader and by just using the FileReader you could only read the File character by character.

So using a BufferedReader around the FileReader is a very efficient way of reading contents line by line from the FileReader.

Reading the file:

With the FileReader and the BufferedReader properly setup, all that is left is reading from the file. The code below is simple and self-explanatory.

// Read through the entire file
String currentLineFromFile = bufferedReader.readLine(); // throws IOException
while(currentLineFromFile != null)
{

    // Add a carriage return (line break) to preserve the file formatting.
    textFromFile.append(carriageReturn + currentLineFromFile);
    currentLineFromFile = bufferedReader.readLine(); // throws IOException

}

The above code uses a StringBuilder (textFromFile) to collect the contents of the file. With this code, all the contents of the file is collected into the StringBuilder which can be retrieved by textFromFile.toString()

Displaying the output:

The output from the StringBuilder can just be simple printed on the out stream by

System.out.println( textFromFile.toString());

For the full Java source code listing and a download please go to the next page...

 

 

Comments

Post new comment

  • You can enable syntax highlighting of source code with the following tags: <code>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Search Engines will index and follow ONLY links to allowed domains.

More information about formatting options

Reading from a character based file using the FileReader | Our website now yours! - Currenlty Java focussed.

Reading from a character based file using the FileReader

One of the common problems we always hit is the File IO. This article quickly takes you through the very simple steps involved in how to read from a character based file (text file) and display its contents using your Java code with a working example that you can download.

This article focuses on how to read from a character based file rather than binary files.

FileReader:

The Java SDK provided a very handy utility class called the FileReader (java.io.FileReader) to quickly access character based files. In the most simple case, all you need to is to instantiate the FileReader with a StringThe String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Below are all the articles related to String. which is the path to the file that you want to read. It is as simple as

FileReader fileReader = new FileReader(filePath); // throws FileNotFoundException

Note that instantiating a FileReader using a file path will throw a FileNotFoundException exception if the file itself does not exist, or is a directory/folder , or any other problems that might prevent the file from being accessed/read.

BufferedReader:

Once the FileReader is created with the file path, we are now ready to read the contents of the file. To do this we can use the BufferedReader (java.io.BufferedReader) class. A BufferedReader can be used a wrapper around the FileReader to read line by line. Without the BufferedReader and by just using the FileReader you could only read the File character by character.

So using a BufferedReader around the FileReader is a very efficient way of reading contents line by line from the FileReader.

Reading the file:

With the FileReader and the BufferedReader properly setup, all that is left is reading from the file. The code below is simple and self-explanatory.

// Read through the entire file
String currentLineFromFile = bufferedReader.readLine(); // throws IOException
while(currentLineFromFile != null)
{

    // Add a carriage return (line break) to preserve the file formatting.
    textFromFile.append(carriageReturn + currentLineFromFile);
    currentLineFromFile = bufferedReader.readLine(); // throws IOException

}

The above code uses a StringBuilder (textFromFile) to collect the contents of the file. With this code, all the contents of the file is collected into the StringBuilder which can be retrieved by textFromFile.toString()

Displaying the output:

The output from the StringBuilder can just be simple printed on the out stream by

System.out.println( textFromFile.toString());

For the full Java source code listing and a download please go to the next page...

 

 

Comments

Post new comment

  • You can enable syntax highlighting of source code with the following tags: <code>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Search Engines will index and follow ONLY links to allowed domains.

More information about formatting options