Write a Text File

WriteFile inputs Strings until the user types "X".  Each String is written into a text-file, one String on each line.  

Important Commands

PrintWriter file
  new PrintWriter(new FileWriter(filename));

Opens a text-file for writing.

file.println(info)
Writes a String into the text-file, ending with an end-of-line character.

try...
catch (IOException e)

Handles possible IOException errors, such as trying to write into a read-only file.

import java.io.*;
All file classes and methods are contained in java.io.*.

Comments

Text-file IO is straightforward, similar to other languages.

The program must handle the IOExceptions - Java requires this.  An alternative is to "throw" the errors, but in longer programs this results in a long chain of throws, up to the top level of the program.  Try..Catch is probably simpler (and better).  

The practice of simply displaying Java's standard Exception errors is not very user friendly - real programs should handle errors more productively.

Some care may be required in some operating systems, as the "\n" does not use the same byte-codes in all OS's.  That means a file written in one OS might not be readable in another.  For example:

   Linux :   \n = (char)10

   Windows:   \n = (char)13 + (char)10