Source of TextFileInputDemo.java


  1: //TextFileInputDemo.java
  2: 
  3: import java.io.File;
  4: import java.io.FileNotFoundException;
  5: import java.util.Scanner;
  6: 
  7: public class TextFileInputDemo
  8: {
  9:     public static void main(String[] args)
 10:     {
 11:         String fileName = "out.txt";
 12:         Scanner inputStream = null;
 13:         System.out.println("The file " + fileName + 
 14:             "\ncontains the following lines:\n");
 15:         try
 16:         {
 17:             inputStream = new Scanner(new File(fileName));
 18:         }
 19:         catch(FileNotFoundException e)
 20:         {
 21:             System.out.println("Error opening the file " + fileName + ".");
 22:             System.exit(0);
 23:         }
 24:         while (inputStream.hasNextLine())
 25:         {
 26:             String line = inputStream.nextLine();
 27:             System.out.println(line);
 28:         }
 29:         inputStream.close();
 30:     }
 31: }
 32: