Source of TextFileInputDemo.java


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