• August 30, 2014
  • Core Java
  • Comments Off on Input output (I/O) in java

Input output (I/O) in java

In java Input output operations are based on the concept of stream. The java.io package contains all the required classes to perform I/O operations.

A stream is defined as a sequence of data.

Automatically created streams in java:

  1. 1. System.in: standard input stream.
  2. 2. System.out: standard output stream.
  3. 3. System.err: standard error.

Input stream:

InputStream is used to read data from a source like file, peripheral device etc.

InputStream class:

InputStream class is the super class of all classes representing an input stream. It is an abstract class.

Commonly used methods of InputStream class:

1. read(): Reads the next byte of data from the input stream. It returns -1 at the end of file.

Syntax: public abstract int read()throws IOException.

2. available(): Returns an estimate of the number of bytes that can be read from the current input stream.

Syntax: public int available()throws IOException.

3. close(): It is used to close the current input stream.

Syntax: public void close()throws IOException.

Output Stream:

OutputStream is used for writing data to a destination like file, peripheral device etc.

OutputStream class:

OutputStream class is the super class of all classes representing an output stream. It is an abstract class.

Commonly used methods of OutputStream class:

1. write(int): This method is used to write a byte to the current output stream.

Syntax: public void write(int)throws IOException.

2. write(byte[]): This method is used to write an array of byte to the current output stream.

Syntax: public void write(byte[])throws IOException.

3. flush():flushes the current output stream.

Syntax: public void flush()throws IOException.

4. close(): This method is used to close the current output stream.

Syntax: public void close()throws IOException.
 
Next Topic: FileInputStream and FileOutputStream in java with example.
Previous Topic: Synchronized block in java with example.

Related Topics:

Input output (I/O) in java.
FileInputStream and FileOutputStream in java with example.
Byte Streams in java with example.
DataInputStream and DataOutputStream in java with example.
BufferedInputStream and BufferedOutputStream in java with example.
FileReader and FileWriter in java with example.
How To Check If A File Exists In Java.
Serialization in java with example.
Transient in java with example.

 


Comments are closed.