<Previous    Back to Start  Contents   Next>


1.2 Exam Only: The java.io package

For this section, you may find the following tables useful. They list the various kinds of Streams and Readers/Writers, along with what they "connect to", typically their constructor arguments.

Low Level Streams
Input Streams Arguments Output Streams Arguments
FileInputStream File FileOutputStream File
ByteArrayInputStream byte[] ByteArrayOutputStream (Creates an array)

High Level Streams
Input Streams Arguments Output Streams Arguments
FilterInputStream InputStream FilterOutputStream OutputStream
DataInputStream
Implements DataInput
InputStream DataOutputStream
Implements DataOutput
OutputStream
BufferedInputStream InputStream BufferedOutputStream OutputStream

Low-level Readers/Writers
Readers Arguments Writers Arguments
FileReader File FileWriter File
CharArrayReader char[] CharArrayWriter Creates an array of chars
StringReader String StringWriter Creates a String

High-level Readers/Writers
Readers Arguments Writers Arguments
BufferedReader Reader BufferedWriter Writer
InputStreamReader InputStream OutputStreamWriter OutputStream


Write code that uses objects of the file class to navigate a file system.

Methods that support navigation:

boolean exists() - Tests if this File exists.

String getAbsolutePath() - Returns the absolute pathname of the file represented by this object.

String getCanonicalPath() - Returns the canonical form of this File object's pathname.

String getName() - Returns the name of the file represented by this object.

String getParent() - Returns the parent part of the pathname of this File object, or null if the name has no parent part.

String getPath() - Returns the pathname of the file represented by this object.

boolean isDirectory() - Tests if the file represented by this File object is a directory.

boolean isFile() - Tests if the file represented by this File object is a "normal" file.

String[] list() - Returns a list (an array of Strings) of the files in the directory specified by this File object.

Note: The File class does not provide a method to change the current working directory.

Other methods worth being aware of:

mkdir() - Creates a directory whose pathname is specified by this File object.

delete() - Deletes the file specified by this object.

length() - Returns the length of the file represented by this File object.

renameTo(File) - Renames the file specified by this File object to have the pathname given by the File argument.

canRead() & canWrite() - Return true if the file is readable/writeable.


Write code that uses objects of the classes InputStreamReader and OutputStreamWriter to translate between Unicode and either platform default or ISO 8859-1 character encodings.

The following constructor for InputStreamReader allows you to specify the encoding scheme:

InputStreamReader(InputStream in, String enc)

Create an InputStreamReader that uses the named character encoding.
Parameters:
in - An InputStream
enc - Name of encoding to be used

Encoding scheme string "8859_1" is ASCII.

The constructor for OutputStreamWriter is similar, but the first argument is an OutputStream, obviously.

Readers (such as InputStreamReader) have the following methods for reading characters:

read() -Read a single character.

read(char[] cbuf) -Read characters into an array.

read(char[] cbuf, int off, int len) -Read characters into a portion of an array.

Writers (such as OutputStreamWriter) have the following methods for writing characters:

write(char[] cbuf) -Write an array of characters.

write (char[] cbuf, int off, int len) -Write a portion of an array of characters.

write(int c) -Write a single character.

write(String str) -Write a string.

write(String str, int off, int len) -Write a portion of a string.


Destinguish between conditions under which platform default encoding conversion should be used and conditions under which a specific conversion should be used.

To avoid corruption, text should be read using the same encoding scheme that it was written with. Therefore, in general platform default encoding may be used if the data is written on the same computer as it is going to be read. However if the text is going to be sent from one computer to another across a network, or if a text file is created on a different computer (for example files that are installed as part of an application), then a specific encoding scheme should be used at both ends.


Select valid constructor arguments for FilterInputStream and FilterOutputStream subclasses from a list of classes in the java.io.package.

The correct constructor argument for FilterInputStream is an instance of a subclass of InputStream. Similarily, the correct constructor argument for FilterOutputStream is an instance of a subclass of OutputStream.


Write appropriate code to read, write and update files using FileInputStream, FileOutputStream, and RandomAccessFile objects.

See a textbook, the Java tutorials or Javadocs if you are unsure of this area. Here is some brief information on RandomAccessFile:

RandomAccessFile is not part of the i/o stream or reader/writer hierarchies. The constructors are:

RandomAccessFile(String file, String mode)
RandomAccessFile(File file, String mode)

Important: For the RandomAccessFile constructor, the mode argument must either be equal to "r" or "rw". Do not be fooled in the exam by credible sounding, but non-existent options like "r+w" or "w".

RandomAccessFile has a pointer which determines where to read/write in the file. getFilePointer() returns the current position in the file, in bytes, and seek(long position) sets the pointer to a specific location. There is also a length() method. This class implements the DataInput and DataOutput interfaces, which have read or write methods (respectively) for all the primitives, strings, and byte arrays ( readFully and write() respectively). The DataInput interface also has skipBytes() method. Consult the Java API Docs for more on these interfaces.


Describe the permanent effects on the file system of constructing and using FileInputStream, FileOutputStream, and RandomAccessFile objects.

RandomAccessFile will create an empty file if it is constructed as "rw". Constructing a FileOutputStream can create an empty file.

FileInputStream of course, never causes a file to be created or modified.


<Previous    Back to Start  Contents   Next>

©1999, 2000, 2002 Dylan Walsh.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with the Invariant Sections being the disclaimer, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".