3.2 Reading & Writing FilesHomepage « Java5 Certification « 3.2 Reading & Writing Files
In this lesson we investigate how we can use classes within the java.io package
to read from files, write to files and use the BufferedReader
, BufferedWriter
,
File
, FileReader
, FileWriter
and PrintWriter
sometimes in unison to create a software solution.
Lets take a look at the points outlined at the Oracle Website for this part of the certification.
- Section 3: API Contents
- Given a scenario involving navigating file systems, reading from files, or writing to files, develop the correct solution using the following classes (sometimes in combination), from java.io: BufferedReader,BufferedWriter, File, FileReader, FileWriter and PrintWriter.
Character Streams
Top
Character streams are defined within two class hierarchies, one for input and one for output:
- The
Writer
class is the abstract superclass of all character output streams - The
Reader
class is the abstract superclass of all character input streams
These classes define the characteristics that are common to character input and character output streams, which are implemented in the concrete subclasses of each hierarchy.
Character Output Stream Hierarchy
Top
The diagram below shows the classes in the character output stream hierarchy of which the Writer
class is the abstract superclass.:

Class | Description |
---|---|
Writer | Abstract character stream superclass which describes this type of output stream. |
BufferedWriter | Buffered output character stream. |
CharArrayWriter | Character buffer output stream. |
FilterWriter | Abstract character stream for writing filtered streams. |
OuputStreamWriter | Output Stream that acts as a bridge for encoding byte streams from character streams. |
FileWriter | Output stream for writing characters to a file. |
PipedWriter | Piped character output stream. |
PrintWriter | Convenience output character stream to add functionality to another stream, an example being to print to the console using print() and println() . |
StringWriter | Output stream for writing characters to a string. |
Click on one of the class links in the table above to see usage for the character output streams required for certification.
Character Input Stream Hierarchy
Top
The diagram below shows the classes in the character input stream hierarchy of which the Reader
class is the abstract superclass.:

Class | Description |
---|---|
Reader | Abstract character stream superclass which describes this type of input stream. |
BufferedReader | Buffered input character stream. |
LineNumberReader | Input character stream that keeps a count of line numbers. |
CharArrayReader | Character buffer input stream. |
FilterReader | Abstract character stream for reading filtered streams. |
PushbackReader | Character stream reader containing functionality to return characters to the input stream. |
InputStreamReader | Input Stream that acts as a bridge for decoding byte streams into character streams. |
FileReader | Input stream for reading characters from a file. |
PipedReader | Piped character input stream. |
StringReader | Input stream for reading characters from a string. |
Click on one of the class links in the table above to see usage for the character input streams required for certification.
Other Java I/O Classes
Top
The diagram below shows some other pertinent classes in the java.io
package not covered in the byte and character streams above:

Class | Description |
---|---|
File | Abstract representation of file and directory pathnames. |
FileDescriptor | Opaque handle to the underlying machine-specific structure. |
RandomAccessFile | Allows reading and writing of bytes to a random access file. |
StreamTokenizer | Input stream to be parsed into 'tokens'. |
Click on the class link in the table above to see usage for the File
class required for certification.
The java.io.File
Class
Top
We will finish this lesson with a talk about the File
class that exists within the Java.io
package and how we use objects of this class to represent an actual file and directory pathname that
may or may not exist already on a hard drive. A File
object doesn't contain the file in question or any data associated with the file, it just acts like a pointer to said file and can be a relative or absolute pathname:
- Relative URL - The common use of a relative URL is by omitting the protocol and server name, as documents generally reside on the same server. So this would be directoryName/fileName.extension.
For example the relative url images/j5icon.jpg - Absolute URL - An absolute url is the complete address of the resource.
For example the absolute url http://java5tutor.info/images/j5icon.jpg
Related Java5 Tutorials
API Contents - The java.io.File
Class
API Contents - The java.io.PrintWriter
Class
API Contents - The java.io.BufferedReader
Class
API Contents - The java.io.FileReader
Class
API Contents - The java.io.BufferedWriter
Class
API Contents - The java.io.FileWriter
Class