//http://www.cs.wisc.edu/~cs302/io/JavaIO.html

import java.io.*;

public class FileOutputDemo
{
	// Create a single shared BufferedReader for keyboard input
	private static BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) );
		
	public static void main (String [] args) throws IOException
	{
		System.out.print( "Enter the filename: " );   // Prompt the user for a file name
		String fileName = stdin.readLine();           // get a file name from the user
		
		// Create a FileWriter attached to a file named "out.txt".
		// The second parameter sets whether or not new data
		// will be appended to the end of the file or the beginning.
		// false means that this file will be overwritten.
		FileWriter fw = new FileWriter( fileName, false );
		
		// Create a PrintWriter that automatically flushes data
		// to the output file whenever the println method is used.
		PrintWriter pw = new PrintWriter( fw, true );
		
		// Buffer some data to write to the file (doesn't actually write until flush)
		pw.print( "Some test data that will be written when flush is called.");
		
		// Flush all buffered data to the file.
		pw.flush();
		
		String data = "";
		
		for(int i = 0; i < 5; i++)
		{
			System.out.print("Please enter some data to print to file: ");
			data = stdin.readLine();
			
			// Write some data and automatically flush it to the file.
			pw.println( data );
		}
		
		// Close the PrintWriter for added safety.
		pw.close();
	}
}

