import java.io.*;

public class FileIOExample
{
	public static void main (String[] args)
	{
		//create keyboard reader
		BufferedReader rdr = new BufferedReader(
			    new InputStreamReader(System.in));

		//read text
		String line = null;
		System.out.println ("Enter a line of text and press Enter.");

		try
		{
			line = rdr.readLine();
		}
		catch (IOException e)
		{
			System.out.println ("Error occurred");
			System.exit(1);
		}

		System.out.println (line);				//display text

		PrintWriter wrtr = null;					//create file writer

		String outfile = "test.txt";				//write text to file

		try
		{
			wrtr = new PrintWriter(
				      new BufferedWriter(
					  new FileWriter(outfile)));
			wrtr.println(line);
		}
		catch (IOException e)
		{
			System.out.println("IO Error: " + e.getMessage());
			System.exit(1);
		}
		finally
		{
			wrtr.close();
		}
	}
}
