Java Input and Output



Java Input and Output :

  1. Java I/O is the process to produce the output based on the input given
  2. Most of the Java I/O Streams classes are in java.io package.
  3. Most of the Java File I/O classes are in the java.nio.file package.

Java Stream Concept :

Consider the example of the water pipeline, Water flows in the pipeline then we can say that its stream of water. Similarly when the sequence of the data flows from input device to output device and vice versa then we can say that its a stream of data.

  1. I/O Stream represents input source or an output destination.
  2. Different kinds of sources and destinations are – disk files, devices, other programs, and memory arrays.
  3. Different kinds of data are – byte,data types, characters, objects

Three I/O Streams :

Stream Purpose
System.out standard output stream
System.in standard input stream
System.err standard error

Java Output Stream :

  1. Java application uses an output stream to write data to a destination
  2. Output Stream may be a file , an array , peripheral device or socket.

io-outs

OutputStream class

  1. OutputStream class is an abstract class.
  2. OutputStream is superclass of all classes representing an output stream of bytes.
  3. An output stream accepts output bytes and sends them to pool
Method Description
public void write(int)throws IOException Used to write a byte to the current output stream.
public void write(byte[])throws IOException Used to write an array of byte to the current output stream.
public void flush()throws IOException Used to flush the current output stream.
public void close()throws IOException Used to close the current output stream.

Java Input Stream :

  1. Java application uses an input stream to read data from source
  2. Input Stream may be a file, disk, object or peripheral device or socket.

io-ins

InputStream class

  1. InputStream class is an abstract class.
  2. InputStream is the superclass of all classes representing an input stream of bytes.

Commonly used methods of InputStream class

Method Description
public abstract int read() throws IOException reads the next byte of data from the input stream.It returns -1 at the end of file.
public int available() throws IOException returns an estimate of the number of bytes that can be read from the current input stream.
public void close() throws IOException is used to close the current input stream.