Scanning a File


You need to scan a file with more fine-grained resolution than the readLine( ) method of the BufferedReader class and its subclasses (discussed in Section 9.12).


Use a StreamTokenizer, readline( ) and a StringTokenizer, regular expressions (Chapter 4), or one of several scanning tools such as JavaCC.


While you could, in theory, read the file a character at a time and analyze each character, that is a pretty low-level approach. The read( ) method in the Reader class is defined to return int, so that it can use the time-honored value -1 (defined as EOF in Unix <stdio.h> for years) to indicate that you have read to the end of the file.

void doFile(Reader is) {
    int c;
    while ((  )) != -1) {

The cast to char is interesting. The program will compile fine without it, but may not print correctly (depending on the contents of the file).

We discussed the StringTokenizer class extensively in Section 3.3. The combination of readLine( ) and StringTokenizer provides a simple means of scanning a file. Suppose you need to read a file in which each line consists of a name like “user@host.domain”, and you want to split the lines into the user part and the host address part. You could use this:

// protected void process(LineNumberReader is) { String s = null; try { while ((s = is.readLine( )) != null) { StringTokenizer st = new StringTokenizer(s, "@", true); String user = (String)st.nextElement( ); st.nextElement( ...

Get Java Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.