0% found this document useful (0 votes)
5 views17 pages

IO Information

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views17 pages

IO Information

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Java I/O

Java I/O (Input and Output) is used to process the


input and produce the output.

Java uses the concept of a stream to make I/O


operation fast. The java.io package contains all the
classes required for input and output operations.

We can perform file handling in Java by Java I/O API.


Stream
A stream is a sequence of data. In Java, a stream is
composed of bytes. It's called a stream because it is
like a stream of water that continues to flow.

In Java, 3 streams are created for us automatically. All


these streams are attached with the console.

1) System.out: standard output stream


2) System.in: standard input stream
3) System.err: standard error stream
the code to print output and an error message to the console.

System.out.println("simple message");
System.err.println("error message");

the code to get input from console.

int i=System.in.read(); //returns ASCII code of 1st character


System.out.println((char)i); //will print the character
There are 2 Streams in Java- Byte Stream and
Character Stream.
•Byte streams are used to perform input and
output of 8-bit bytes. Byte streams are
useful when we want to read/write binary
data.
•Character stream is used to perform input
and output operations of 16-bit Unicode.
ByteStream Classes in Java
• ByteStream classes are used to read bytes from
the input stream and write bytes to the output
stream.
• The ByteStream classes are divided into two
types of classes, i.e., InputStream and
OutputStream. These classes are abstract and
the super classes of all the Input/Output stream
classes.
OutputStream vs InputStream
The explanation of OutputStream and InputStream classes are
given below:

OutputStream
Java application uses an output stream to write data to a
destination; it may be a file, an array, peripheral device or
socket.

InputStream
Java application uses an input stream to read data from a
source; it may be a file, an array, peripheral device or socket.
the working of Java OutputStream and InputStream by the
figure given below.
InputStream Class
The InputStream class provides methods to read bytes from a
file, console or memory.
SN Class Description
1 BufferedInputStream This class provides methods to read bytes from the buffer.
2 ByteArrayInputStream This class provides methods to read bytes from the byte array.

3 DataInputStream This class provides methods to read Java primitive data types.

4 FileInputStream This class provides methods to read bytes from a file.


5 FilterInputStream This class contains methods to read bytes from the other input streams,
which are used as the primary source of data.

6 ObjectInputStream This class provides methods to read objects.


7 PipedInputStream This class provides methods to read from a piped output stream to
which the piped input stream must be connected.
8 SequenceInputStream This class provides methods to connect multiple Input Stream and read
data from them.
The InputStream class contains various methods to read the data from an input stream. These
methods are overridden by the classes that inherit the InputStream class.
SN Method Description
1 int read() This method returns an integer, an integral representation of the next available
byte of the input. The integer -1 is returned once the end of the input is
encountered.
2 int read (byte buffer []) This method is used to read the specified buffer length bytes from the input and
returns the total number of bytes successfully read. It returns -1 once the end of
the input is encountered.
3 int read (byte buffer [], This method is used to read the 'nBytes' bytes from the buffer starting at a
int loc, int nBytes) specified location, 'loc'. It returns the total number of bytes successfully read
from the input. It returns -1 once the end of the input is encountered.

4 int available () This method returns the number of bytes that are available to read.
5 Void mark(int nBytes) This method is used to mark the current position in the input stream until the
specified nBytes are read.
6 void reset () This method is used to reset the input pointer to the previously set mark.
7 long skip (long nBytes) This method is used to skip the nBytes of the input stream and returns the total
number of bytes that are skipped.
8 void close () This method is used to close the input source. If an attempt is made to read even
after the closing, IOException is thrown by the method.
OutputStream Class
The OutputStream is an abstract class that is used to write 8-bit bytes to
the stream. It is the superclass of all the output stream classes.
SN Class Description
1 BufferedOutputStream This class provides methods to write the bytes to the buffer.

2 ByteArrayOutputStream This class provides methods to write bytes to the byte array.

3 DataOutputStream This class provides methods to write the java primitive data types.

4 FileOutputStream This class provides methods to write bytes to a file.

5 FilterOutputStream This class provides methods to write to other output streams.

6 ObjectOutputStream This class provides methods to write objects.

7 PipedOutputStream It provides methods to write bytes to a piped output stream.

8 PrintStream It provides methods to print Java primitive data types.


The OutputStream class provides various methods to write bytes to the output streams.
The methods are given in the following table.

SN Method Description

1 void write (int i) This method is used to write the specified single byte to the output
stream.
2 void write (byte buffer [] ) It is used to write a byte array to the output stream.

3 Void write(bytes buffer[], It is used to write nByte bytes to the output stream from the buffer
int loc, int nBytes) starting at the specified location.

4 void flush () It is used to flush the output stream and writes the pending buffered
bytes.
5 void close () It is used to close the output stream. However, if we try to close the
already closed output stream, the IOException will be thrown by this
method.
CharacterStream Classes in Java
• The java.io package provides CharacterStream classes to overcome
the limitations of ByteStream classes, which can only handle the 8-bit
bytes and is not compatible to work directly with the Unicode
characters. CharacterStream classes are used to work with 16-bit
Unicode characters. They can perform operations on characters, char
arrays and Strings.
• However, the CharacterStream classes are mainly used to read
characters from the source and write them to the destination.
• For this purpose, the CharacterStream classes are divided into two
types of classes, I.e., Reader class and Writer class.
Reader Class
Reader class is used to read the 16-bit characters from the input stream. However, it is an abstract class and can't be
instantiated, but there are various subclasses that inherit the Reader class and override the methods of the Reader class.

All methods of the Reader class throw an IOException. The subclasses of the Reader class are given in the following table.

SN Class Description
1. BufferedReader This class provides methods to read characters from the buffer.

2. CharArrayReader This class provides methods to read characters from the char array.

3. FileReader This class provides methods to read characters from the file.

4. FilterReader This class provides methods to read characters from the underlying
character input stream.

5 InputStreamReader This class provides methods to convert bytes to characters.

6 PipedReader This class provides methods to read characters from the connected piped
output stream.

7 StringReader This class provides methods to read characters from a string.


The Reader class methods are given in the following table.

SN Method Description
1 int read() This method returns the integral representation of the next character present in the input.
It returns -1 if the end of the input is encountered.
2 int read(char buffer[]) This method is used to read from the specified buffer. It returns the total number of
characters successfully read. It returns -1 if the end of the input is encountered.
3 int read(char buffer[], int This method is used to read the specified nChars from the buffer at the specified location.
loc, int nChars) It returns the total number of characters successfully read.
4 void mark(int nchars) This method is used to mark the current position in the input stream until nChars
characters are read.
5 void reset() This method is used to reset the input pointer to the previous set mark.
6 long skip(long nChars) This method is used to skip the specified nChars characters from the input stream and
returns the number of characters skipped.
7 boolean ready() This method returns a boolean value true if the next request of input is ready. Otherwise,
it returns false.
8 void close() This method is used to close the input stream. However, if the program attempts to access
the input, it generates IOException.
Writer Class
Writer class is used to write 16-bit Unicode characters to the output stream. The
methods of the Writer class generate IOException.

Like Reader class, Writer class is also an abstract class that cannot be instantiated;
therefore, the subclasses of the Writer class are used to write the characters onto
the output stream. The subclasses of the Writer class are given in the below table.
SN Class Description
1 BufferedWriter This class provides methods to write characters to the buffer.
2 FileWriter This class provides methods to write characters to the file.
3 CharArrayWriter This class provides methods to write the characters to the character array.
4 OutpuStreamWriter This class provides methods to convert from bytes to characters.
5 PipedWriter This class provides methods to write the characters to the piped output
stream.
6 StringWriter This class provides methods to write the characters to the string.
To write the characters to the output stream, the Write class provides various methods given in the following table.

SN Method Description
1 void write() This method is used to write the data to the output stream.
2 void write(int i) This method is used to write a single character to the output
stream.
3 Void write(char buffer[]) This method is used to write the array of characters to the
output stream.
4 void write(char buffer [],int loc, This method is used to write the nChars characters to the
int nChars) character array from the specified location.
5 void close () This method is used to close the output stream. However, this
generates the IOException if an attempt is made to write to the
output stream after closing the stream.

6 void flush () This method is used to flush the output stream and writes the
waiting buffered characters.

You might also like