Pages

IO Streams

Java Input And Output Stream
Java Input And Output Stream

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.

Types of Stream

There are two kinds of Streams :

1. InPutStream : The InputStream is used to read data from a source.

2. OutPutStream : The OutputStream is used for writing data to a destination.


Java Provided Standard 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.

OutputStream Class

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

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) int read(byte[] b, int off, int len) method reads up to len bytes of data from the input stream into an array of bytes.
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.

OutputStream Hierarchy
  • OutputStream
    • FileOutputStream
    • ByteArrayOutputStream
    • PipedOutputStream
    • ObjectOutputStream
    • FilterOutputStream
      • PrintStream
      • DataOutputStream
      • BufferedOutputStream

InputStream Hierarchy
  • InputStream
    • FileInputStream
    • ByteArrayInputStream
    • PipedInputStream
    • ObjectInputStream
    • FilterInputStream
      • PrintStream
      • DataInputStream
      • BufferedInputStream

FileInputStream Class

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.