Home Segments Top Top Previous Next

482: Mainline

At this point, you are ready to start assembling a traditional-approach program, such as the following, which simply displays each line of the input file. The program reads each line from the buffered reader using the readLine method. Reading continues until the readLine method returns null or a string with no characters, detected by way of the equals method with an empty-string ordinary argument:

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); 
  BufferedReader buffer = new BufferedReader(reader); 
  String line; 
  int nextSpace, x, y, z; 
  while ((line = buffer.readLine()) != null && !line.equals("")) { 
   System.out.println("Line read: " + line); 
  } 
  stream.close(); 
  return; 
 } 
} 
--- Data ---
 4  7  3 
 8  8  7 
 2 10  5 
--- Result --- 
Line read:  4  7  3 
Line read:  8  8  7 
Line read:  2 10  5