Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of

Essence OUT.

Essence IN.

In this example a text-file on disk is made using and read using

When writing, the file is created anew, overwriting a previous version, if there is one.

FileWriter itself doesn't have many convenient write methods, but PrintWriter has one in particular that we want to use : println !

The PrintWriter object is created wrapping the FileWriter object ....

and used to write to the file .

The actual byte transport is handled by FileWriter.

Close the file properly. Passed on to FileWriter.

The file is read using FileReader, accessing it through BufferedReader's convenient readLine method :

The readLine method returns null if there's nothing left to read : end-of-file.

Most methods in this context can throw exceptions, like: file can't be found, no more disk space, etc.

This explains why much of the code is in try blocks with catches, as demanded by the compiler .


To append to an already existing file use this, where the boolean indicates appending.

This is the only way to update a file using a Writer class. Real I/O can NOT be done with Reader and Writer classes, because you have to choose one or the other, IN or OUT.




All Reader and Writer classes are SEQUENTIAL, start at the beginning and work your way toward the end.


A browser program should allow this class to save and retrieve in your local file system if the class itself had been loaded from that same file system.

However, your browser receives this class from our web-site, which makes it untrusted.

Well, you can trust us, of course, but your browser just doesn't and raises a SecurityException, as it should.

Another way to run code such as this is locally, using the appletviewer. A stand-alone application poses no problem.


All Writer and Reader classes, there are more, are meant to handle java Strings.


By specifying only a filename and no path, the file written ends up in a default directory.

If you want to specify an MSWin path, use double slashes. This is because the backslash in a String is used to mark special characters, like \n, \t, etc.

UNIX forward slashes don't pose a problem.


The technique shown, opening and closing the file at EACH button click, is not something you would do in a real-world application. Open the file once initially and close it when all has been done that needs to be done.