The java.io package contains nearly every class you might ever need to perform input and output in Java. An Input And Output Stream represents an input source or an output destination. A stream can represent many different kinds of sources and destinations, including disk files, devices, other programs, and memory arrays. Streams support many different kinds of data, including simple bytes, primitive data types, localized characters, and objects.
Some streams simply pass on data, others manipulate and transform the data in useful ways. No matter how they work internally, all streams present the same simple model to programs that use them, A stream is a sequence of data. We will see streams that can handle all kinds of data, from primitive values to advanced objects. The data source and data destination pictured above can be anything that holds, generates, or consumes data. Obviously this includes disk files, but a source or destination can also be another program, a peripheral device, a network socket, or an array. A program uses an input stream to read data from a source, one item at a time. A program uses an output stream to write data to a destination, one item at time.
There are two kinds of Streams :
All the programming languages provide support for standard I/O where the user's program can take input from a keyboard and then produce an output on the computer screen. Java provides the following Three Streams are created for us automatically and all these streams are attached with the console.
| Standard Streams | Description |
|---|---|
| System Output | System Output is used to output the data produced by the user's program and usually a computer screen is used for standard output stream and represented as System.out. |
| System Input | System Input is used to feed the data to user's program and usually a keyboard is used as standard input stream and represented as System.in. |
| System Error | System Error is used to output the error data produced by the user's program and usually a computer screen is used for standard error stream and represented as System.err. |
A Closeable is an Interface. A Closeable Interface extends AutoCloseable Interface. A Closeable is a source or destination of data that can be closed. The close method is invoked to release resources that the object is holding (such as open files).
| Methods | Description |
|---|---|
| void close() | void close() method closes this stream and releases any system resources associated with it. |
A DataInput is an Interface. The DataInput interface provides for reading bytes from a binary stream and reconstructing from them data in any of the Java primitive types. There is also a facility for reconstructing a String from data in modified UTF-8 format. It is generally true of all the reading routines in this interface that if end of file is reached before the desired number of bytes has been read, an EOFException (which is a kind of IOException) is thrown. If any byte cannot be read for any reason other than end of file, an IOException other than EOFException is thrown. In particular, an IOException may be thrown if the input stream has been closed.
| Methods | Description |
|---|---|
| boolean readBoolean() | boolean readBoolean() method reads one input byte and returns true if that byte is nonzero, false if that byte is zero. |
| byte readByte() | byte readByte() method reads and returns one input byte. |
| char readChar() | char readChar() method reads two input bytes and returns a char value. |
| double readDouble() | double readDouble() method reads eight input bytes and returns a double value. |
| float readFloat() | float readFloat() method reads four input bytes and returns a float value. |
| void readFully(byte[] b) | void readFully(byte[] b) method reads some bytes from an input stream and stores them into the buffer array b. |
| void readFully(byte[] b, int off, int len) | void readFully(byte[] b, int off, int len) method reads len bytes from an input stream. |
| int readInt() | int readInt() method reads four input bytes and returns an int value. |
| String readLine() | String readLine() method reads the next line of text from the input stream. |
| long readLong() | long readLong() method reads eight input bytes and returns a long value. |
| short readShort() | short readShort() method reads two input bytes and returns a short value. |
| int readUnsignedByte() | int readUnsignedByte() method reads one input byte, zero-extends it to type int, and returns the result, which is therefore in the range 0 through 255. |
| int readUnsignedShort() | int readUnsignedShort() method reads two input bytes and returns an int value in the range 0 through 65535. |
| String readUTF() | String readUTF() method reads in a string that has been encoded using a modified UTF-8 format. |
| int skipBytes(int n) | int skipBytes(int n) method makes an attempt to skip over n bytes of data from the input stream, discarding the skipped bytes. |
The API says that the class File is "An abstract representation of file and directory pathnames." The File class isn't used to actually read or write data; it's used to work at a higher level, making new empty files, searching for files, deleting files, making directories, and working with paths.
This class is used to read character files. Its read() methods are fairly low-level, allowing you to read single characters, the whole stream of characters, or a fixed number of characters. FileReaders are usually wrapped by higher-level objects such as BufferedReaders, which improve performance and provide more convenient ways to work with the data.
This class is used to make lower-level Reader classes like FileReader more efficient and easier to use. Compared to FileReaders, BufferedReaders read relatively large chunks of data from a file at once, and keep this data in a buffer. When you ask for the next character or line of data, it is retrieved from the buffer, which minimizes the number of times that time-intensive, file read operations are performed. In addition, BufferedReader provides more convenient methods such as readLine(), that allow you to get the next line of characters from a file.
This class is used to write to character files. Its write() methods allow you to write character(s) or Strings to a file. FileWriters are usually wrapped by higher-level Writer objects such as BufferedWriters or PrintWriters, which provide better performance and higher-level, more flexible methods to write data.
This class is used to make lower-level classes like FileWriters more efficient and easier to use. Compared to FileWriters BufferedWriters write relatively large chunks of data to a file at once, minimizing the number of times that slow, file writing operations are performed. The BufferedWriter class also provides a newLine() method to create platform-specific line separators automatically.
This class has been enhanced significantly in Java 5. Because of newly created methods and constructors (like building a PrintWriter with a File or a String), you might find that you can use PrintWriter in places where you previously needed a Writer to be wrapped with a FileWriter and/or a BufferedWriter. New methods like format(), printf(), and append() make PrintWriters very flexible and powerful.
This new, Java 6 convenience class provides methods to read input from the console and write formatted output to the console.
OutputStream class is an Abstract Class. OutputStream class extends Object Class. OutputStream class implements Closeable and Flushable. OutputStream class has contains Single Constructor that means Normal Constructor. OutputStream is the super class of all classes representing an output stream of bytes. An output stream accepts output bytes and sends them to some sink. Java application uses an output stream to write data to a destination. it may be a file, an array, peripheral device or socket.
| Methods | Description |
|---|---|
| void close() | void close() method closes this output stream and releases any system resources associated with this stream. |
| void flush() | void flush() method flushes this output stream and forces any buffered output bytes to be written out. |
| void write(byte[] b) | void write(byte[] b) method writes b.length bytes from the specified byte array to this output stream. |
| void write(byte[] b, int off, int len) | void write(byte[] b, int off, int len) method writes len bytes from the specified byte array starting at offset off to this output stream. |
| abstract void write(int b) | abstract void write(int b) method writes the specified byte to this output stream. |
InputStream class is an Abstract Class. InputStream class extends Object Class. InputStream class implements Closeable. InputStream class has contains Single Constructor that means Normal Constructor. InputStream is the super class of all classes representing an input stream of bytes. Applications that need to define a subclass of InputStream must always provide a method that returns the next byte of input.
| Methods | Description |
|---|---|
| int available() | int available() method returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. |
| void close() | void close() method closes this input stream and releases any system resources associated with the stream. |
| void mark(int readlimit) | void mark(int readlimit) method marks the current position in this input stream. |
| boolean markSupported() | boolean markSupported() method tests if this input stream supports the mark and reset methods. |
| int read(byte[] b) | int read(byte[] b) method reads some number of bytes from the input stream and stores them into the buffer array b. |
| int read(byte[] b, int off, int len) | long skip(long n) method skips over and discards n bytes of data from this input stream. |
| void reset() | void reset() method repositions this stream to the position at the time the mark method was last called on this input stream. |
| long skip(long n) | long skip(long n) method skips over and discards n bytes of data from this input stream. |
FileInputStream class is Class. FileInputStream class extends InputStream Class. FileInputStream class implements Closeable and AutoCloseable. FileInputStream class has contains three parameter Construcots. A FileInputStream obtains input bytes from a file in a file system. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.
| Methods | Description |
|---|---|
| void close() | void close () method Closes this file input stream and releases any system resources associated with the stream. |
| int available() | void available () method Returns an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. |
| protected void finalize() | void finalize() method Ensures that the close method of this file input stream is called when there are no more references to it. |
| FileChannel getChannel() | void getChannel method Returns the unique FileChannel object associated with this file input stream. |
| FileDescriptor getFD() | void getFD() method Returns the FileDescriptor object that represents the connection to the actual file in the file system being used by this FileInputStream. |
| int read() | int read() method Reads a byte of data from this input stream. |
| int read(byte[] b) | int read(byte[] b) method Reads up to b.length bytes of data from this input stream into an array of bytes. |
| int read(byte[] b, int off, int len) | int read(byte[] b, int off, int len) method Reads up to len bytes of data from this input stream into an array of bytes. |
| long skip(long n) | long skip(long n) method Skips over and discards n bytes of data from the input stream. |