Contents:
BufferedOutputStream
BufferedReader
BufferedWriter
ByteArrayInputStream
ByteArrayOutputStream
CharArrayReader
CharArrayWriter
CharConversionException
DataInput
DataInputStream
DataOutput
DataOutputStream
EOFException
Externalizable
File
FileDescriptor
FileInputStream
FilenameFilter
FileNotFoundException
FileOutputStream
FileReader
FileWriter
FilterInputStream
FilterOutputStream
FilterReader
FilterWriter
InputStream
InputStreamReader
InterruptedIOException
InvalidClassException
InvalidObjectException
IOException
LineNumberInputStream
LineNumberReader
NotActiveException
NotSerializableException
ObjectInput
ObjectInputStream
ObjectInputValidation
ObjectOutput
ObjectOutputStream
ObjectStreamClass
ObjectStreamException
OptionalDataException
OutputStream
OutputStreamWriter
PipedInputStream
PipedOutputStream
PipedReader
PipedWriter
PrintStream
PrintWriter
PushbackInputStream
PushbackReader
RandomAccessFile
Reader
SequenceInputStream
Serializable
StreamCorruptedException
StreamTokenizer
StringBufferInputStream
StringReader
StringWriter
SyncFailedException
UnsupportedEncodingException
UTFDataFormatException
WriteAbortedException
Writer
The package java.io contains the classes that handle fundamental input and output operations in Java. The I/O classes can be grouped as follows:
I/O in Java is based on streams. A stream represents a flow of data or a channel of communication. Java 1.0 supports only byte streams. The InputStream class is the superclass of all of the Java 1.0 byte input streams, while OutputStream is the superclass of all the byte output streams. The drawback to these byte streams is that they do not always handle Unicode characters correctly.
As of Java 1.1, java.io contains classes that represent character streams. These character stream classes handle Unicode characters appropriately by using a character encoding to convert bytes to characters and vice versa. The Reader class is the superclass of all the Java 1.1 character input streams, while Writer is the superclass of all character output streams.
The InputStreamReader and OutputStreamWriter classes provide a bridge between byte streams and character streams. If you wrap an InputStreamReader around an InputStream object, the bytes in the byte stream are read and converted to characters using the character encoding scheme specified by the InputStreamReader. Likewise, you can wrap an OutputStreamWriter around any OutputStream object so that you can write characters and have them converted to bytes.
As of Java 1.1, java.io also contains classes to support object serialization. Object serialization is the ability to write the complete state of an object to an output stream, and then later recreate that object by reading in the serialized state from an input stream. The ObjectOutputStream and ObjectInputStream classes handle serializing and deserializing objects, respectively.
The RandomAccessFile class is the only class that does not use a stream for reading or writing data. As its name implies, RandomAccessFile provides nonsequential access to a file for both reading and writing purposes.
The File class represents a file on the local file system. The class provides methods to identify and retrieve information about a file.
Figure 11.1 shows the class hierarchy for the java.io package. The java.io package defines a number of standard I/O exception classes. These exception classes are all subclasses of IOException, as shown in Figure 11.2.
java.io.BufferedInputStream
java.io.FilterInputStream
None
None
JDK 1.0 or later
A BufferedInputStream object provides a more efficient way to read just a few bytes at a time from an InputStream. BufferedInputStream object use a buffer to store input from an associated InputStream. In other words, a large number of bytes are read from the underlying stream and stored in an internal buffer. A BufferedInputStream is more efficient than a regular InputStream because reading data from memory is faster than reading it from a disk or a network. All reading is done directly from the internal buffer; the disk or network needs to be accessed only occasionally to fill up the buffer.
You should wrap a BufferedInputStream around any InputStream whose read() operations may be time consuming or costly, such as a FileInputStream.
BufferedInputStream provides a way to mark a position in the stream and subsequently reset the stream to that position, using mark() and reset().
public class java.io.BufferedInputStream extends java.io.FilterInputStream { // Variables protected byte[] buf; protected int count; protected int marklimit; protected int markpos; protected int pos; // Constructors public BufferedInputStream(InputStream in); public BufferedInputStream(InputStream in, int size); // Instance Methods public synchronized int available(); public synchronized void mark(int readlimit); public boolean markSupported(); public synchronized int read(); public synchronized int read(byte[] b, int off, int len); public synchronized void reset(); public synchronized long skip(long n); }
The buffer that stores the data from the input stream.
A placeholder that marks the end of valid data in the buffer.
The maximum number of bytes that can be read after a call to mark() before a call to reset() fails.
The position of the stream when mark() was called. If mark() has not been called, this variable is -1.
The current position in the buffer, or in other words, the index of the next character to be read.
The input stream to buffer.
This constructor creates a BufferedInputStream that buffers input from the given InputStream, using a buffer with the default size of 2048 bytes.
The input stream to buffer.
The size of buffer to use.
This constructor creates a BufferedInputStream that buffers input from the given InputStream, using a buffer of the given size.
The number of bytes that can be read without blocking.
If any kind of I/O error occurs.
FilterInputStream.available()
This method returns the number of bytes that can be read without having to wait for more data to become available. The returned value is the sum of the number of bytes remaining in the object's buffer and the number returned as the result of calling the available() method of the underlying InputStream object.
The maximum number of bytes that can be read before the saved position becomes invalid.
FilterInputStream.mark()
This method causes the BufferedInputStream to remember its current position. A subsequent call to reset() causes the object to return to that saved position, and thus reread a portion of the buffer.
The boolean value true.
FilterInputStream.markSupported()
This method returns true to indicate that this class supports mark() and reset().
The next byte of data or -1 if the end of the stream is encountered.
If any kind of I/O error occurs.
FilterInputStream.read()
This method returns the next byte from the buffer. If all the bytes in the buffer have been read, the buffer is filled from the underlying InputStream and the next byte is returned. If the buffer does not need to be filled, this method returns immediately. If the buffer needs to be filled, this method blocks until data is available from the underlying InputStream, the end of the stream is reached, or an exception is thrown.
public synchronized int read(byte b[], int off, int len) throws IOException
An array of bytes to be filled from the stream.
An offset into the byte array.
The number of bytes to read.
The actual number of bytes read or -1 if the end of the stream is encountered immediately.
If any kind of I/O error occurs.
FilterInputStream.read(byte[], int, int)
This method copies bytes from the internal buffer into the given array b, starting at index off and continuing for up to len bytes. If there are any bytes in the buffer, this method returns immediately. Otherwise the buffer needs to be filled; this method blocks until the data is available from the underlying InputStream, the end of the stream is reached, or an exception is thrown.
If there was no previous call to this BufferedInputStream's mark method, or the saved position has been invalidated.
FilterInputStream.reset()
This method sets the position of the BufferedInputStream to a position that was saved by a previous call to mark(). Subsequent bytes read from this BufferedInputStream will begin from the saved position and continue normally.
The number of bytes to skip.
The actual number of bytes skipped.
If any kind of I/O error occurs.
FilterInputStream.skip()
This method skips n bytes of input. If the new position of the stream is still within the data contained in the buffer, the method returns immediately. Otherwise the skip() method of the underlying stream is called. A subsequent call to read() forces the buffer to be filled.
Method |
Inherited From |
Method |
Inherited From |
---|---|---|---|
clone() |
Object |
close() |
FilterInputStream |
equals(Object) |
Object |
finalize() |
Object |
getClass() |
Object |
hashCode() |
Object |
notify() |
Object |
notifyAll() |
Object |
read(byte[]) |
FilterInputStream |
toString() |
Object |
void wait() |
Object |
void wait(long) |
Object |
void wait(long, int) |
Object |
FilterInputStream, InputStream, IOException
This HTML Help has been published using the chm2web software. |