Java #11 – Writing a Text File

Java #11 – Writing a Text File

Writing a text file in Java is exactly
like writing to the console with System.out.println, except
you have to do some more setup. In the class “WriteSimpleFile”, we have a static method
“selectTextFile” that will allow the user to pick a file
using a dialog box. The JFileChooser class allows us
to display a file save dialog, just like you’d see in any other
application. First, we create an object of the
JFileChooser class. Next, we want to look for files
ending in dot TXT or dot Java, do we create a
FileNameExtensionFilter object and set that to be the filter for the
object chooser. We display the dialog by calling
chooser’s showSaveDialog method. We pass null as a parameter,
because we don’t have a GUI window. Running the program, we see that
displays a normal file save dialog, showing only java and text files, and
allows us to either select save or cancel. Looking back at the code, we see
that showSaveDialog returns a value, indicating whether we selected save
or cancel. If the return value is equal to the constant
APPROVE_OPTION, then we have entered a filename, and attempt to create and return a
PrintStream with the chosen file. If not, the user selected “cancel” and
we simply return null. It’s possible that the user might type
in an invalid filename (for example, a non-existent directory
or read-only filename). In that case, a FileNotFound exception will be
thrown when we try to create the scanner. We catch that exception, and display an error dialog using a
static method in the JOptionPane class. Note that for a static method, we use
name of class dot name of method and don’t first have to create a
JOptionPane object. The do while true method guarantees
we will keep displaying the file save dialog until either the user enters a valid
filename or picks “Cancel”. Now let’s look at the main method for
this class. Before calling the selectTextFile
method, we set the look and feel for the
application to the system look and feel. This way, the file open dialog looks
like other applications on your system, rather than the Java cross-platform
dialog. We use the static method setLookAndFeel
in the UIManager class to accomplish this, passing it the result of the static method
getSystemLookAndFeelClassName. Since setLookAndFeel might throw
an exception (although not with the parameter we
passed), we are required to enclose it in a
try-catch block. We then call selectTextFile. If it doesn’t return null, we’ll write a
short Haiku to the file using println on the
outputStream. This is just like calling
System.out.println, except we use the object outputStream
instead of the object System.out. As with System.out, we could output
ints or doubles or Strings, or any other object. Once we’re done, we close the file. The file would be closed automatically
when the program finished, but it is a good habit to get into for the times we might want to write a
file and read it later in the same program. We’ll now run the program and enter
a valid filename. Opening that file in notepad, you’ll see that it has written the text
to the file.

5 thoughts on “Java #11 – Writing a Text File”

  1. Thanks for your tutorials.

    I do however have a question and maybe this WriteSimpleFile could be expanded;

    1) It does not get same directory as the .java file
    2) Type a name for file, however no extension is added if I don't type full filename
    3) Add multiple file Filters, ie. Java or TXT being shown, if so then save with that extension

    Just a couple ideas as I'm new to Java, thanks again

Leave a Reply

Your email address will not be published. Required fields are marked *