Reading Text Files

Submitted by: 

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;
  2. import;
  3. import;
  4. import;

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.

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.


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.