/*****************************************************************
** FileWrite - program to demonstrate writing to a file
**
** Text entered on the keyboard is written to a disk file called
** junk.txt  A sample dialogue follows:
**
**    PROMPT>java FileWrite
**    Enter some text on the keyboard...
**    (^z to terminate)
**    A friend in need is a friend in deed
**    ^z
**    PROMPT>
**
** (c) Scott MacKenzie, 2000                             
******************************************************************/
import java.io.*;

class FileWrite
{
   public static void main(String[] args) throws IOException
   {
      // open keyboard for input (call it 'stdin')
      BufferedReader stdin =
         new BufferedReader(new InputStreamReader(System.in), 1);

      // Let's call the output file 'junk.txt'
      String s = "junk.txt";

      // check if output file exists
      File f = new File(s);
      if (f.exists())
      {
         System.out.print("Overwrite " + s + " (y/n)? ");
         if(!stdin.readLine().toLowerCase().equals("y"))
            return;
      }

      // open file for output
      PrintWriter outFile =
         new PrintWriter(new BufferedWriter(new FileWriter(s)));

      // inform the user what to do
      System.out.println("Enter some text on the keyboard...");
      System.out.println("(^z to terminate)");

      // read from keyboard, write to file output stream
      String s2;
      while ((s2 = stdin.readLine()) != null)
         outFile.println(s2);

      // close disk file
      outFile.close(); 
   }
}

