0% found this document useful (0 votes)
53 views22 pages

IO Streams

This document discusses I/O streams in Java. It describes how streams provide an abstraction for input and output through devices like files and networks. There are two main types of streams - byte streams for binary data and character streams for text. Common stream classes include FileInputStream, FileOutputStream, BufferedReader, and InputStreamReader which are used to read from and write to files and console input.

Uploaded by

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

IO Streams

This document discusses I/O streams in Java. It describes how streams provide an abstraction for input and output through devices like files and networks. There are two main types of streams - byte streams for binary data and character streams for text. Common stream classes include FileInputStream, FileOutputStream, BufferedReader, and InputStreamReader which are used to read from and write to files and console input.

Uploaded by

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

I/O Streams

This topic includes:


• The i/o package supports Java’s basic I/O (input/output)
system,
• Including File I/O.
Streams in Java:
• Java programs perform I/O through streams.
• A Stream is an abstraction that either produces or consumes
information.
• A Stream is linked to a physical device by the Java I/O system.
• All streams behave in the same manner, even they are linked
to different devices.

• The streams are of: Input Stream and Output stream


• An input stream can abstract many different kinds of input:
from a disk file, a keyboard, or a network socket.
• An output Stream may refer to Console, a disk file or a
network connection.
• Streams have a clean way to deal with input/output without
having every part of our code understand the difference
between a keyboard and a network as an example.

• Java implements streams within class hierarchies defined in


the java.io package.
Byte Streams and Character Streams:
• Java defines two types of streams:
1. Byte streams
2. Character streams

• Byte Streams provide a convenient means for handling input


and output of bytes.
For example, when reading and writing binary data .
• Character Streams provide a convenient means for handling
input and output of characters.
The Byte Stream class
The Byte Streams are defined by using two class hierarchies:
1. InputStream and
2. OutputStream

• The above two are abstract classes which define several key
methods and most important are: read() and write()
• They are overridden by derived Stream classes
The character Stream classes:
• Character Streams are defined by using two hierarchies. At
the top are two abstract classes:
1. Reader and
2. Writer

These abstract classes handle Unicode character streams.


Predefined Streams:
• All java programs automatically import the java.lang package
• This package defines a class “System”, which encapsulates
several aspects of the run-time environment.
• Example: obtain current time and settings of various
properties associated with the system
• System also contains three predefined stream variables: in,
out and err
• These fields are declared as public, static and final within
System.
Creating a file using FileOutputStream:
• FileOutputStream class belongs to byte stream and stores the
data in the form of individual bytes.
• It can be used to create text files.

• We know that a file represents storage of data on a secondary


storage media like hard disk or CD.
• the following steps are to be followed to create a text file that
stores som echaracters or text.
1. First of all, we should read data from the keyboard. For this
purpose, we should attach the keyboard to some input stream
class. The code for using DataInputStream class for reading data
from keyboard:
DataInputStream dis=new DataInputStream(System.in);
Here, System.in represents the keyboard which is linked with
DataInputStream object, that is dis.
3. The next step is to read data from DataInputStream and write it into
FileOutputStream. It means read data from dis object and write it into fout
object, as shown here:
ch=(char)dis.read(); //read one character into ch

fout.write(ch); //write ch into file.

4.Finally, any file should be closed after performing input or output


operations on it, else the data of the file may be corrupted. Closing the file
is done by closing the associated streams. For example,
fout.close(); will close the FileOutputStream, hence there is no way to write
data into the file.
Reading data from a file using FileInputStream:
• FileOutputStream class belongs to byte stream and stores the
data in the form of individual bytes.
• It can be used to create text files.

• We know that a file represents storage of data on a secondary


storage media like hard disk or CD.
• the following steps are to be followed to create a text file that
stores som echaracters or text.
Reading Console Input:
• In java 1.0, the only way to perform console input was to use
a byte stream
• But the preferred method of reading console input is to use a
character-oriented stream, which makes our program easier
to maintain

• In java console input is accomplished by reading from


System.in
• BufferedReader supports a buffered input stream, which is a
wrap of System.in in a BufferedReader object.
BufferedReader(Reader inputReader)

• To obtain InputStreamReader object that is linked to System.in


use the following constructor:
InputStreamReader(InputStream inputStream)
• Putting all together we have:
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));

From the above statement,the execution takes place as br is a


character-based stream that is linked to the Console through
the System.in
File Handling:
• File handling is a mechanism which provides a number of
classes and methods that allow us to read and write files.
• Two of the most often-used stream classes are:
1. FileInputStream and
2. FileOutputStream

The following are the forms:


FileInputStream(String fileName)throws IOException
FileOutputStream(String fileName)throws IOException

You might also like