| Developer's Daily | Java Education |
| front page | java | perl | unix | DevDirectory |
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:
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. |
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:
| String record = null;
try { while ( (record=br.readLine()) != null ) {
} catch (IOException e) {
|
| 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. |
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.
Copyright 1998-2008 DevDaily Interactive, Inc.
All Rights Reserved.