Developer's Daily Java Education
  front page | java | perl | unix | DevDirectory
   
Front Page
Java
Education
Pure Java
Articles
   
 
Opening and reading files with Java JDK 1.1.x
 

Introduction

In an earlier article, we showed you how to open and read files with the classes and methods available in Java JDK 1.0.x.  In this article, we'll update that technique and show you how to open and read files using the classes and methods in Java JDK 1.1.x (and more recent).  As we mentioned in the earlier article, it's very important to know how to open and read from files when you're building Java applications or servlets.
 

Opening a text data file using Java JDK 1.1.x

Rather than duplicate the work of the previous article, we'll move through this article pretty quickly.

Using JDK 1.1.x, you'll typically open a file for read access with the File or FileReader classes.  We discussed the File class briefly in the last article, so this time we'll just open a file directly with the FileReader class.

Opening a file for read access with Java JDK 1.1.x can be done with the following two steps:

  1. Open the file with the FileReader class;
  2. Convert the FileReader input stream with the BufferedReader class;
That's all you need to do now to open a data file for read access.  The FileReader is a subclass of InputStreamReader that is useful if you want to read text from a data file.  The BufferedReader applies buffering to the character input stream which improves the efficiency of the reading process - meaning you can read your data files more quickly.

Listing 1 shows what these steps look like in Java code.

 
FileReader fr = new FileReader("mydata.txt"); 
BufferedReader br = new BufferedReader(fr);
 
 
Listing 1:   These are the steps I normally take to open a text file for read access using Java JDK 1.1.x.
 
 
Need to use "try/catch"
 
Because both the FileReader and BufferedReader objects can throw exceptions, you'll need to use the typical "try/catch" syntax around your statements.  If you haven't seen this before, fear not, because we'll show you a full working example shortly.
 


Reading from the data file

Once the data file is open, reading from the file is a simple matter.  I normally just use the readLine() method of the BufferedReader class to read in a full line at a time.  A single read statement will look like this:

You can use a call like this any time you want to read a line of data from the file as a String object.  If you'd like to read the entire data file like this, you can simple put the readLine() statement into a while loop.  This technique is shown in Listing 2.

 
String record = null; 

try { 

   while ( (record=br.readLine()) != null ) { 
      // 
      // put your logic here to work with "record" 
      // 
   } 

} catch (IOException e) { 
   // 
   // put your error-handling code here 
   // 
} 
 

 
Listing 2:   This listing shows a procedure that can be used to read every line of information from a data file using the readLine() method inside of a while loop. 
 
 
A working example

Now that we've shown the separate parts of the open and read operations, let's put it all together in a simple example.  Listing 3 shows a completely working example of a technique we often use to open a data file, read it's contents, and close the file when we're finished with it.  We implement our technique here in a method named readMyFile.

 


// FileReadTest.java
// Copyright 1998 DevDaily Interactive, Inc.  All Rights Reserved.

import java.io.*;

  class FileReadTest { 

     //--------------------------------------------------< main >--------//

     public static void main (String[] args) {
        FileReadTest t = new FileReadTest();
        t.readMyFile();
     } 


     //--------------------------------------------< readMyFile >--------//

     void readMyFile() { 

        String record = null;
        int recCount = 0; 

        try { 

	   FileReader fr     = new FileReader("mydata.txt");
           BufferedReader br = new BufferedReader(fr);

           record = new String();
           while ((record = br.readLine()) != null) {
              recCount++;
              System.out.println(recCount + ": " + record); 
           } 

        } catch (IOException e) { 
           // catch possible io errors from readLine()
           System.out.println("Uh oh, got an IOException error!");
           e.printStackTrace();
        }

     } // end of readMyFile()

  } // end of class
   

 
Listing 3:   This listing shows a complete working example of a program that (a) opens a data file, (b) reads records of information from the file, and (c) prints each record of information from the file to standard output, placing a line number before each record.
 

 
Download the example source code

If you like the example shown in Listing 3 and would like to use it in your applications, or just want to try it on your system, you can download the source by clicking here.  Once the file is displayed in your browser you can select the File | Save As ... option of your browser to save the code to your local filesystem.
 



What's Related


Copyright 1998-2008 DevDaily Interactive, Inc.
All Rights Reserved.