Thursday, 4 December 2008

Old School java.io

Java's BufferedReader extends the abstract Reader class, which reads character streams.

import java.io.BufferedReader;
import java.io.FileReader;

try {
BufferedReader br = new BufferedReader( new FileReader( someFile ) );
while ( br.ready() ) {
String line = br.readLine();
// processLine(line)
}

br.close();
} catch (Exception e) {}

read() will read one character, readline() will read a line. Don't forget to close the reader when you're done. The BufferedReader has two constructors, one that takes a "regular" Reader object, and the other that takes a Reader and the size of the buffer.

No comments:

Blog Archive