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 Streamsgo to top of page 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 Hierarchygo to top of page Top

The diagram below shows the classes in the character output stream hierarchy of which the Writer class is the abstract superclass.:

character input output hierarchy
Class Description
WriterAbstract character stream superclass which describes this type of output stream.
BufferedWriterBuffered output character stream.
CharArrayWriterCharacter buffer output stream.
FilterWriterAbstract character stream for writing filtered streams.
OuputStreamWriterOutput Stream that acts as a bridge for encoding byte streams from character streams.
FileWriterOutput stream for writing characters to a file.
PipedWriterPiped character output stream.
PrintWriterConvenience output character stream to add functionality to another stream, an example being to print to the console using print() and println().
StringWriterOutput 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 Hierarchygo to top of page Top

The diagram below shows the classes in the character input stream hierarchy of which the Reader class is the abstract superclass.:

character input stream hierarchy
Class Description
ReaderAbstract character stream superclass which describes this type of input stream.
BufferedReaderBuffered input character stream.
LineNumberReaderInput character stream that keeps a count of line numbers.
CharArrayReaderCharacter buffer input stream.
FilterReaderAbstract character stream for reading filtered streams.
PushbackReaderCharacter stream reader containing functionality to return characters to the input stream.
InputStreamReaderInput Stream that acts as a bridge for decoding byte streams into character streams.
FileReaderInput stream for reading characters from a file.
PipedReaderPiped character input stream.
StringReaderInput 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 Classesgo to top of page Top

The diagram below shows some other pertinent classes in the java.io package not covered in the byte and character streams above:

Other java.io classes
Class Description
FileAbstract representation of file and directory pathnames.
FileDescriptorOpaque handle to the underlying machine-specific structure.
RandomAccessFileAllows reading and writing of bytes to a random access file.
StreamTokenizerInput 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 Classgo to top of page 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:

  1. 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
  2. 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

go to home page Java5 Tutor Homepage go to home page Top