
/*

	Tom DeDonno 

	Demonstrates PrintWriter methods 
		constructor
		creating and appending
		println		print
		close		flush (write JVM buffer to disk)
                
         Program writes 100 sequential  numbers to disk
         5 numbers per each line
        
        allows use option of appending to file or creating new file
 */

import java.io.*; // for PrintWriter and FileInputStream

public class TextFileOutputDemo1 {

	public static void main( String[] args ) {

		PrintWriter outputStream = (PrintWriter)null;
		String fileName = "out1.txt";
		

		try {
                   boolean fAppend = true; //set to true to add to file
                  //Fales existing file is truncated, default is false
                  
                  System.out.println( "Enter A to Append to File; " +
                  "N to create new file" );
                  char ans = SavitchIn.readLineNonWhiteChar( );
                  fAppend = (ans == 'A' || ans == 'a' );
		  // Why must outputStream be defined above
		  outputStream =
		  new PrintWriter(
                  new FileOutputStream( fileName, fAppend ) );
                       
		} catch( IOException e ) {
                        //FileNotFoundException is child of IOException
			System.out.println( e.getMessage() );
			System.out.println( "Error creating " + fileName );
			System.exit(0);
		} // End Catch
			
			
		for( int count = 1; count <= 100 ; count++ ) {

			outputStream.print( count + "," );
                        
                        if( count%5 == 0 ) 
                          outputStream.println( );
                        
			if( count%10 == 0 ) 
			  outputStream.flush();
                        
                        if( count%11 == 0 ) {
                          System.out.println( "Data should be in file" );
                          SavitchIn.readLine( );
                          }
			}

		outputStream.close( );
		} //end main
		
} // end class textFileOutputDemo
		
