
/*

	Tom DeDonno page 583

	Demonstrates PrintWriter class Text File Output

	PrintWriter

		import java.io not built in like System.out
		creat a class object not static like System.out
		create file name uses FileOutputStream
		write to file
		close file
		exceptions
			no use checkError() Method 

	FileInputStream exceptions
		throws IOExceptions
 */

import java.io.*; // for PrintWriter and FileInputStream

public class TextFileOutputDemo {

	public static void main( String[] args ) {

		PrintWriter outputStream = (PrintWriter)null;
		String fileName = "out.txt";  
                //Disk File to be created or Truncated

		try {

			outputStream =
			new PrintWriter( new FileOutputStream( fileName ) );
                        // FileOutputStream JVM stream file description
                        // Two related files one in JVM one on disk
		
		} catch( IOException e ) {
                        //FileNotFoundException is child of IOException
			System.out.println( e.getMessage() );
			System.out.println( "Error creating " + fileName );
			System.exit(0);
		} // End Catch
			
			
		System.out.println( "Enter three lines of text" );
		String line = (String)null;

		for( int count = 1; count <= 3 ; count++ ) {

			line = SavitchIn.readLine();
			outputStream.println( count + ":" + line );
			}

                // file contents etc exist in JVM/Buffer may not be on disk
                // need to physical close the JVM file - done with it
		outputStream.close( );
		System.out.println( "Lines Added to " + fileName );
		} //end main
		
} // end class textFileOutputDemo
		
