One of the commonly used classes in the java.io package is File. This class is somewhat misleadingly named, as it represents a filename (or directory name), rather than a file itself. Because files (and directories) have different naming conventions under different operating systems, Java provides the File class to try to hide some of those differences. The File class also defines various methods for operating on files as a whole: deleting files, creating directories, listing directories, querying the size and modification time of a file, and so on. While the File class provides methods to manipulate directories and the files within those directories, it doesn't provide any methods that manipulate the contents of the files. In other words, it doesn't provide any way to read or write the bytes or characters that are contained in files. In Java, sequential file I/O is performed through a stream abstraction. (Random-access file I/O is performed with the RandomAccessFile class, but sequential I/O is much more common.) A stream is simply an object from which data can be read sequentially or to which data can be written sequentially. The bulk of the java.io package consists of stream classes: there are 40 of them. InputStream and OutputStream and their respective subclasses are objects for reading and writing streams of bytes, whereas Reader and Writer and their subclasses are objects for reading and writing streams of Unicode characters. In addition to these stream classes, the java.util.zip package defines another eight input and output byte streams for data compression and decompression. Tables Table 3-1 through Table 3-4 summarize the stream classes available in java.io and java.util.zip. Table 3-1. Byte input streams Byte input stream | Description |
---|
BufferedInputStream | Reads a buffer of bytes from an InputStream, and then returns bytes from the buffer, making small reads more efficient. | ByteArrayInputStream | Reads bytes sequentially from an array. | CheckedInputStream | This java.util.zip class computes a checksum of the bytes it reads from an InputStream. | DataInputStream | Reads binary representations of Java primitive types from an InputStream. | FileInputStream | Reads bytes sequentially from a file. | FilterInputStream | The superclass of byte input stream filter classes. | GZIPInputStream | This java.util.zip class uncompresses GZIP-compressed bytes it reads from an InputStream. | InflaterInputStream | The superclass of GZIPInputStream and ZipInputStream. | InputStream | The superclass of all byte input streams. | LineNumberInputStream | This class is deprecated as of Java 1.1; use LineNumberReader instead. | ObjectInputStream | Reads binary representations of Java objects and primitive values from a byte stream. This class is used for the deserialization of objects. | PipedInputStream | Reads bytes written to the PipedOutputStream to which it is connected. Used in multithreaded programs. | PushbackInputStream | Adds a fixed-size pushback buffer to an input stream, so that bytes can be unread. Useful with some parsers. | SequenceInputStream | Reads bytes sequentially from two or more input streams, as if they were a single stream. | StringBufferInputStream | This class is deprecated as of Java 1.1; use StringReader instead. | ZipInputStream | This java.util.zip class uncompresses entries in a ZIP file. | Table 3-2. Character input streams Character input stream | Description |
---|
BufferedReader | Reads a buffer of characters from a Reader, and then returns characters from the buffer, making small reads more efficient. | CharArrayReader | Reads characters sequentially from an array. | FileReader | Reads characters sequentially from a file. An InputStreamReader subclass that reads from an automatically created FileInputStream. | FilterReader | The superclass of character input stream filter classes. | InputStreamReader | Reads characters from a byte input stream. Converts bytes to characters using the encoding of the default locale, or a specified encoding. | LineNumberReader | Reads lines of text and keeps track of how many have been read. | PipedReader | Reads characters written to the PipedWriter to which it is connected. Used in multithreaded programs. | PushbackReader | Adds a fixed-size pushback buffer to a Reader, so that characters can be unread. Useful with some parsers. | Reader | The superclass of all character input streams. | StringReader | Reads characters sequentially from a string. | Table 3-3. Byte output streams Byte output stream | Description |
---|
BufferedOutputStream | Buffers byte output for efficiency; writes to an OutputStream only when the buffer fills up. | ByteArrayOutputStream | Writes bytes sequentially into an array. | CheckedOutputStream | This java.util.zip class computes a checksum of the bytes it writes to an OutputStream. | DataOutputStream | Writes binary representations of Java primitive types to an OutputStream. | DeflaterOutputStream | The superclass of GZIPOutputStream and ZipOutputStream. | FileOutputStream | Writes bytes sequentially to a file. | FilterOutputStream | The superclass of all byte output stream filters. | GZIPOutputStream | This java.util.zip class outputs a GZIP-compressed version of the bytes written to it. | ObjectOutputStream | Writes binary representations of Java objects and primitive values to an OutputStream. Used for the serialization of objects. | OutputStream | The superclass of all byte output streams. | PipedOutputStream | Writes bytes to the PipedInputStream to which it is connected. Used in multithreaded programs. | PrintStream | Writes a textual representation of Java objects and primitive values. Deprecated except for use by the standard output stream System.out as of Java 1.1. In other contexts, use PrintWriter instead. | ZipOutputStream | This java.util.zip class compresses entries in a ZIP file. | Table 3-4. Character output streams Character output stream | Description |
---|
BufferedWriter | Buffers output for efficiency; writes characters to a Writer only when the buffer fills up. | CharArrayWriter | Writes characters sequentially into an array. | FileWriter | Writes characters sequentially to a file. A subclass of OutputStreamWriter that automatically creates a FileOutputStream. | FilterWriter | The superclass of all character output stream filters. | OutputStreamWriter | Writes characters to a byte output stream. Converts characters to bytes using the encoding of the default locale, or a specified encoding. | PipedWriter | Writes characters to the PipedReader to which it is connected. Used in multithreaded programs. | PrintWriter | Writes textual representations of Java objects and primitive values to a Writer. | StringWriter | Writes characters sequentially into an internally created StringBuffer. | Writer | The superclass of all character output streams. | |