[go: up one dir, main page]

0% found this document useful (0 votes)
17 views23 pages

Chapter 11 - Input Output Stream

Uploaded by

dileepdilee8747
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)
17 views23 pages

Chapter 11 - Input Output Stream

Uploaded by

dileepdilee8747
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/ 23

Chapter 10

Input and Output Stream


Stream:
“Stream is a flow of information from a source to a destination”.
A stream is a sequence of data.
Ex:-
❖ A keyboard to a monitor.
❖ A monitor to a file on a hard disk.
❖ A file on a hard disk to a monitor.
❖ A file to a printer.
Stream Advantages:
Streams are mainly used to move data from one place to another.
The java.io package contains a large No. of stream classes that provide capabilities processing all
type of data.

Classification of Streams
Streams are basically divided into 2 categories:
❖ Byte Stream
❖ Character stream
Byte stream or Binary Stream
Byte streams in Java are used to perform input and output operations of 8-bit bytes.
We can store video, audio, characters, etc., by using ByteStream classes.
These classes are part of the java.io package.
The byte streams are defined by using two classes hierarchies.
Input stream class: reads data from the source.
Output Stream class : writes data to a destination.
InputStream Class
The InputStream class provides methods to read bytes from a file, console or memory.
InputStream is an abstract class that provides the framework from which all other input streams
are derived.
The subclasses of InputStream class
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.

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

6 ObjectInputStream This class provides methods to read objects.


Methods of InputStream class

SN Method Description
1 int read() This method returns an integer
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.
3 int read (byte buffer [], int loc, int nBytes) This method is used to read the 'nBytes' bytes from the buffer starting
at a specified location, 'loc'. It returns the total number of bytes
successfully read from the input.
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
6 void reset () This method is used to reset the input pointer to the previously set
mark.
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.
The subclasses of OutputStream class

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.


Methods of OutputStream class

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[],int loc, int It is used to write nByte bytes to the output stream from the
nBytes) buffer starting at the specified location.

4 void flush () It is used to flush the output stream

5 void close () It is used to close the output stream.


Character stream or Text Stream
❖ Character stream handle data in 16 bit bytes.
❖ Using these we can read and write text data only.
❖ They can perform operations on characters, character arrays and strings.
❖ CharacterStream classes are used to overcome the limitations of ByteStream classes, which can
only handle the 8-bit bytes.
❖ 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.

The subclasses of the Reader class


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. InputStreamReader This class provides methods to convert bytes to characters.
5. StringReader This class provides methods to read characters from a string.
The Reader class methods

SN Method Description
1 read() It reads a single character.
2 read(char[] cbuf) It reads characters into an array.
3 ready() It tells whether this stream is ready to be read.
4 reset() It resets the stream.
5 void close() This method is used to close the input stream.
6 mark(int readAheadLimit) It marks the present position in the stream.
Writer Class
❖ Writer class is used to write 16-bit Unicode characters to the output stream.
❖ The subclasses of the Writer class are used to write the characters onto the output stream

The subclasses of the Writer class


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 OutputStreamWriter This class provides methods to convert from bytes to characters.
5. StringWriter This class provides methods to write the characters to the string.
The Write class Methods

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 close () This method is used to close the output stream.
5 void flush () This method is used to flush the output stream and writes the
waiting buffered characters.
Uses of java I/O Classes
❖ File Access
❖ Network access
❖ Reading & writing text
❖ Reading & writing Primitive data types.
❖ Reading & writing Objects.
File Class
❖ The File class of the java.io package is used to perform various operations on files and directories.
❖ A file is a named location that can be used to store related information. For example, main.java is
a Java file that contains information about the Java program.
❖ A directory is a collection of files and subdirectories. A directory inside a directory is known as
subdirectory.
Create a Java File Object
❖ To create an object of File, we need to import the java.io.File package first.
❖ Once we import the package, we can create objects of file as follows
// creates an object of File using the path File file = new File(String pathName);
❖ Here, we have created a file object named file. The object can be used to work with files and
directories
Create a Java File Object
❖ To create an object of File, we need to import the java.io.File package first.
❖ Once we import the package, we can create objects of file as follows
// creates an object of File using the path File file = new File(String pathName);
❖ Here, we have created a file object named file. The object can be used to work with files and
directories
Java File Operation Methods
Random Access File
❖ RandomAccessFile in Java is a class that allows data to be read from and written to at any
location in the file
❖ A random access file consists of a sequence of bytes.
❖ It supports a special pointer known as file pointer.
❖ A file pointer indicates the current position in the file.
❖ It is positioned at one of these bytes in the file and can be moved to any arbitrary position in the
file prior to reading or writing
Constructors of Random Access File
Constructor Description

RandomAccessFile(File Creates a random access file stream with the specified by the File
file, String mode) Object and mode.

RandomAccessFile(String name, Creates a random access file stream with the specified file name and
String mode) mode.

File Modes
r – read-only, w – write only, rw – read-write, rws- the file is opened for read & write.
Example:
RandomAccessFile raf = new RandomAccessFile(java.exe , ’rws’)
Means a random access file java.exe is opened for read & write operation.
End of Chapter

You might also like