11. The java.io Package
Contents:
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. BufferedInputStreamNameBufferedInputStreamSynopsis
DescriptionA 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(). Class Summary
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); } Variablesbufprotected byte[] buf
countprotected int count
marklimitprotected int marklimit
markposprotected int markpos
posprotected int pos
ConstructorsBufferedInputStreampublic BufferedInputStream(InputStream in)
public BufferedInputStream(InputStream in, int size)
Instance Methodsavailablepublic synchronized int available() throws IOException
markpublic synchronized void mark(int readlimit)
markSupportedpublic synchronized boolean markSupported()
readpublic synchronized int read() throws IOException
public synchronized int read(byte b[], int off, int len) throws IOException
resetpublic synchronized void reset() throws IOException
skippublic synchronized long skip(long n) throws IOException
Inherited Methods
See AlsoFilterInputStream, InputStream, IOException | ||||||||||||||||||||||||||||||||
|