I/O Stream Fundamentals:-
- A stream is a flow of data from a source to a sink. Typically, our program is one end of that stream, and some other node (for example, a file) is the other end.
- Source and sinks are also called as input streams and output streams, respectively.
- We can read from an input stream, but we cannot write to it. Conversely, we can write to an output stream, but cannot read from it.
- Following table shows the fundamental stream classes,
|Stream||Byte Streams||Character Streams|
Data within Streams:-
- Java technology supports raw bytes or Unicode characters in streams.
- Stream refers to byte streams and the terms reader and writer refer to character streams.
- More specifically, character input streams are implemented by subclasses of the Reader class and character output streams are implemented by subclasses of the Writer class.
- Byte it ms are implemented by subclasses of the InputStream class and byte output streams are implemented by subclasses of the OutputStream class.
- The following describes the fundamental byte streams.
The InputStream Method:-
- Three methods mentioned below provide access to the data from the input stream:
int read(byte buff)
int read(byte buff, int offset, int length)
- The first method returns an integer, which contains either a byte read from the stream, or a –1, which indicates the end of a file condition.
- The other two methods read a stream into a byte array and return the number of bytes read.
- The two int arguments in the third method indicate a sub range in the target array that needs to be filled.
Note:- For efficiency, always read data in the largest practical block, or use buffered streams.
- When we have finished with a stream, close it. If we have a stack of streams then use filter streams to close the stream at the top the stack. This operation also closes the lower streams.
- This method reports a number of bytes that are immediately available to be read from the stream.
- Real read operation following this call may return more bytes.
long skip(long n)
- It discards the specified number of bytes from the stream.
void mark(int readlimit)
- We can use these methods to perform push-back operations on a stream, if supported by that stream.
- The markSupported() method does return true if the mark() and reset() methods are operational for that particular stream.
- The mark(int) method indicates that the current point in the stream should be noted and a buffer big enough for at least the specified argument number of bytes should be allocated.
- The parameter of the mark(int) method specifies the number of bytes that can be re-read by calling reset() method.
- After performing the subsequent read() operations, and calling the reset() method returns the input stream to the point you marked.
- If you read past the marked buffer, reset() has no meaning.
The OutputStream Methods:-
- The following methods write to the output stream:
void write(byte buff)
void write(byte buff, int off, int length)
- Try to write the data in largest practical block.
- We should close output streams when we have finished with them. Again if we have a stack and close the top one, this closes the rest of the streams.
- Sometimes an output stream accumulates writes before committing them. The flush() method enables we to force writes.
- The following describes the fundamental character streams.
The Reader Methods:-
- Methods mentioned below provide access to the character data from the reader:
int read(char cbuf)
int read(char cbuf, int offset, int length)
- The first method returns an int, which either a Unicode character read from the stream, or a -1, which indicates the end of file condition.
- The other two methods read from a character array and return the number of bytes read.
- The two int arguments in the third method indicate a sub range in target array that needs to be filled.
long skip(long n)
void mark(int readAheadLimit)
- Those methods are analogous to the input stream versions.
The Writers Methods:-
- The following methods write to the writer:
void write(int c)
void write(char cbuf)
void write(char cbuf, int offset, int length)
void write(String string)
void write(String string, int offset, int length)
- Similar to output streams, writers include the close and flush methods.