Source of TextEOFDemo.java


  1: 
  2: import java.io.*;
  3: import java.util.*;
  4: 
  5: /**
  6:  Makes storylines.txt the same as story.txt, 
  7:  but with each line numbered.
  8: */
  9: public class TextEOFDemo
 10: {
 11:    public static void main(String[] args)
 12:    {
 13:       try
 14:       {
 15:          BufferedReader inputStream = 
 16:                new BufferedReader(new FileReader("story.txt"));
 17:          PrintWriter outputStream = 
 18:                new PrintWriter(
 19:                     new FileOutputStream("storylines.txt"));
 20:          int count = 0;
 21:          String line = inputStream.readLine( );
 22:          while (line != null)
 23:          {
 24:             count++;
 25:             outputStream.println(count + " " + line);
 26:             line = inputStream.readLine( ); 
 27:          }
 28:          System.out.println(count 
 29:                           + " lines written to storylines.txt.");
 30:          inputStream.close( );
 31:          outputStream.close( );
 32:       }
 33:       catch(FileNotFoundException e)
 34:       {
 35:          System.out.println("File opening problem.");
 36:       }
 37:       catch(IOException e)
 38:       {
 39:          System.out.println("Error reading from file story.txt.");
 40:       }
 41:    }
 42: }