Source of TextFileInputDemo.java


  1: //TextFileInputDemo.java
  2: 
  3: import java.util.Scanner;
  4: import java.io.File;
  5: import java.io.FileNotFoundException;
  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:                                                    
 16:        try
 17:        {
 18:            inputStream = new Scanner(new File(fileName));
 19:        }
 20:        catch(FileNotFoundException e)
 21:        {
 22:            System.out.println("Error opening the file " + fileName);
 23:            System.exit(0);
 24:        }
 25:            
 26:        while (inputStream.hasNextLine())
 27:        {
 28:            String line = inputStream.nextLine();
 29:            System.out.println(line);
 30:        }
 31:        inputStream.close();
 32:     }
 33: }