Source of TextFileOutputDemo.java


  1: 
  2: import java.io.*;
  3: import java.util.*;
  4: 
  5: public class TextFileOutputDemo
  6: {
  7:     public static void main(String[] args)
  8:     {
  9:         PrintWriter outputStream = null;
 10:         try
 11:         {
 12:             outputStream =
 13:                  new PrintWriter(new FileOutputStream("out.txt"));
 14:         }
 15:         catch(FileNotFoundException e)
 16:         {
 17:             System.out.println("Error opening the file out.txt.");
 18:             System.exit(0);
 19:         }
 20: 
 21:         System.out.println("Enter three lines of text:");
 22:         String line = null;
 23:         Scanner keyboard = new Scanner(System.in);
 24:         int count;
 25:         for (count = 1; count <= 3; count++)
 26:         {
 27:             line = keyboard.nextLine( );
 28:             outputStream.println(count + " " + line);
 29:         }
 30:         outputStream.close( );
 31:         System.out.println("Those lines were written to out.txt.");
 32:     }
 33: }
 34: