Team LiB
Previous Section Next Section

Package java.io

Java 1.0

The java.io package is large, but most of the classes it contains fall into a well-structured hierarchy. Most of the package consists of byte streamssubclasses of InputStream or OutputStream and character streamssubclasses of Reader or Writer. Each of these stream subtypes has a specific purpose, and, despite its size, java.io is a straightforward package to understand and to use. In Java 1.4, the java.io package was complemented by a "New I/O API" defined in the java.nio package and its subpackages. The java.nio package is totally new, although it included some compatibility with the classes in this package. It was designed for high-performance I/O, particularly for use in servers and has a lower-level API than this package does. The I/O facilities of java.io are still quite adequate for most of the I/O required by typical client-side applications.

Before we consider the stream classes that comprise the bulk of this package, let's examine the important nonstream classes. File represents a file or directory name in a system-independent way and provides methods for listing directories, querying file attributes, and renaming and deleting files. FilenameFilter is an interface that defines a method that accepts or rejects specified filenames. It is used by File to specify what types of files should be included in directory listings. RandomAccessFile allows you to read from or write to arbitrary locations of a file. Often, though, you'll prefer sequential access to a file and should use one of the stream classes.

InputStream and OutputStream are abstract classes that define methods for reading and writing bytes. Their subclasses allow bytes to be read from and written to a variety of sources and sinks. FileInputStream and FileOutputStream read from and write to files. ByteArrayInputStream and ByteArrayOutputStream read from and write to an array of bytes in memory. PipedInputStream reads bytes from a PipedOutputStream, and PipedOutputStream writes bytes to a PipedInputStream. These classes work together to implement a pipe for communication between threads.

FilterInputStream and FilterOutputStream are special; they filter input and output bytes. When you create a FilterInputStream, you specify an InputStream for it to filter. When you call the read( ) method of a FilterInputStream, it calls the read( ) method of its InputStream, processes the bytes it reads, and returns the filtered bytes. Similarly, when you create a FilterOutputStream, you specify an OutputStream to be filtered. Calling the write( ) method of a FilterOutputStream causes it to process your bytes in some way and then pass those filtered bytes to the write( ) method of its OutputStream.

FilterInputStream and FilterOutputStream do not perform any filtering themselves; this is done by their subclasses. BufferedInputStream and BufferedOutputStream are filtered streams that provide input and output buffering and can increase I/O efficiency. DataInputStream reads raw bytes from a stream and interprets them in various binary formats. It has various methods to read primitive Java data types in their standard binary formats. DataOutputStream allows you to write Java primitive data types in binary format. The ObjectInputStream and ObjectOutputStream classes are special. These byte-stream classes are used for serializing and deserializing the internal state of objects for storage or interprocess communication.

The byte streams just described are complemented by an analogous set of character input and output streams. Reader is the superclass of all character input streams, and Writer is the superclass of all character output streams. Most of the Reader and Writer streams have obvious byte-stream analogs. BufferedReader is a commonly used stream; it provides buffering for efficiency and also has a readLine( ) method to read a line of text at a time. PrintWriter is another very common stream; its methods allow output of a textual representation of any primitive Java type or of any object (via the object's toString( ) method).

Java 5.0 adds the Closeable and Flushable interfaces to identify types that have close( ) and flush( ) methods. All streams have a close( ) method and implement the Closeable interface. And all byte and character output streams have a flush( ) method and implement Flushable. In a related change, all character output streams (and the byte stream PrintStream ) implement the (new in Java 5.0) interface java.lang.Appendable, making them suitable for use with the java.util.Formatter class. Similarly, all character input streams implement the java.lang.Readable interface, making them suitable for use with the java.util.Scanner class. Finally, both PrintStream and PrintWriter have been enhanced in two ways for Java 5.0. Both now include constructors for creating a stream that writes directly to a file. And both include formatted-text output methods printf( ) and format( ). See java.util.Formatter for details.

Interfaces

public interface Closeable;
public interface DataInput;
public interface DataOutput;
public interface Externalizable extends Serializable;
public interface FileFilter;
public interface FilenameFilter;
public interface Flushable;
public interface ObjectInput extends DataInput;
public interface ObjectInputValidation;
public interface ObjectOutput extends DataOutput;
public interface ObjectStreamConstants;
public interface Serializable;

Classes

public class File implements Serializable, Comparable<File>;
public final class FileDescriptor;
public final class FilePermission extends java.security.Permission implements 
   Serializable;
public abstract class InputStream implements Closeable;
   public class ByteArrayInputStream extends InputStream;
   public class FileInputStream extends InputStream;
   public class FilterInputStream extends InputStream;
      public class BufferedInputStream extends FilterInputStream;
      public class DataInputStream extends FilterInputStream implements 
      DataInput;
      public class LineNumberInputStream extends FilterInputStream;
      public class PushbackInputStream extends FilterInputStream;
   public class ObjectInputStream extends InputStream implements ObjectInput, 
      ObjectStreamConstants;
   public class PipedInputStream extends InputStream;
   public class SequenceInputStream extends InputStream;
   public class StringBufferInputStream extends InputStream;
public abstract static class ObjectInputStream.GetField;
public abstract static class ObjectOutputStream.PutField;
public class ObjectStreamClass implements Serializable;
public class ObjectStreamField implements Comparable<Object>;
public abstract class OutputStream implements Closeable, Flushable;
   public class ByteArrayOutputStream extends OutputStream;
   public class FileOutputStream extends OutputStream;
   public class FilterOutputStream extends OutputStream;
      public class BufferedOutputStream extends FilterOutputStream;
      public class DataOutputStream extends FilterOutputStream implements 
      DataOutput;
      public class PrintStream extends FilterOutputStream implements Appendable, 
      Closeable;
   public class ObjectOutputStream extends OutputStream implements ObjectOutput, 
      ObjectStreamConstants;
   public class PipedOutputStream extends OutputStream;
public class RandomAccessFile implements Closeable, DataInput, DataOutput;
public abstract class Reader implements Closeable, Readable;
   public class BufferedReader extends Reader;
      public class LineNumberReader extends BufferedReader;
   public class CharArrayReader extends Reader;
   public abstract class FilterReader extends Reader;
      public class PushbackReader extends FilterReader;
   public class InputStreamReader extends Reader;
      public class FileReader extends InputStreamReader;
   public class PipedReader extends Reader;
   public class StringReader extends Reader;
public final class SerializablePermission extends java.security.BasicPermission;
public class StreamTokenizer;
public abstract class Writer implements Appendable, Closeable, Flushable;
   public class BufferedWriter extends Writer;
   public class CharArrayWriter extends Writer;
   public abstract class FilterWriter extends Writer;
   public class OutputStreamWriter extends Writer;
      public class FileWriter extends OutputStreamWriter;
   public class PipedWriter extends Writer;
   public class PrintWriter extends Writer;
   public class StringWriter extends Writer;

Exceptions

public class IOException extends Exception;
   public class CharConversionException extends IOException;
   public class EOFException extends IOException;
   public class FileNotFoundException extends IOException;
   public class InterruptedIOException extends IOException;
   public abstract class ObjectStreamException extends IOException;
      public class InvalidClassException extends ObjectStreamException;
      public class InvalidObjectException extends ObjectStreamException;
      public class NotActiveException extends ObjectStreamException;
      public class NotSerializableException extends ObjectStreamException;
      public class OptionalDataException extends ObjectStreamException;
      public class StreamCorruptedException extends ObjectStreamException;
      public class WriteAbortedException extends ObjectStreamException;
   public class SyncFailedException extends IOException;
   public class UnsupportedEncodingException extends IOException;
   public class UTFDataFormatException extends IOException;

    Team LiB
    Previous Section Next Section