Home Segments Top Top Previous Next

494: Mainline

At this point, at last, you have all the machinery needed to produce a series of int values from the information in a file. When you gather that machinery into a program, you have the following, which simply displays the int values, one to a line.

import java.io.*; 
public class Demonstrate { 
 public static void main(String argv[]) throws IOException { 
  FileInputStream stream = new FileInputStream("input.data");  
  InputStreamReader reader = new InputStreamReader(stream); 
  StreamTokenizer tokens = new StreamTokenizer(reader); 
  while (tokens.nextToken() != tokens.TT_EOF) { 
   System.out.println("Integer: " + (int) tokens.nval); 
  } 
  stream.close(); 
 } 
} 
--- Data ---
4  7  3 
--- Result --- 
Integer: 4 
Integer: 7 
Integer: 3 

Note that the close method closes the file input stream, when reading is finished, as prescribed in Segment 475.