Java Unit V
Java Unit V
Java Unit V
Java uses the concept of streams to represent the ordered sequence of data.
A stream is between the program and input and output devices.
A stream in java is a path along which data flows.
It has a sources and a destination.
Java streams are classified into two basic types, namely input stream and output
stream.
Stream Classes
Byte stream classes that provide support for handling i/o operations on bytes.
Character stream classes that provide support for managing i/o operations on
characters.
1
BYTE STREAM CLASSES
For creating and manipulating stream and files for reading and writing bytes.
Java provides two types of byte stream classes.
Input stream classes
Output stream classes.
Input Stream classes
Input stream classes are used to read 8-bit byte include a super class
known as input stream.
The Inputstream includes methods to perform the following tasks
Reading bytes
Closing stream
Marking position in stream
Skipping ahead in stream
Finding the number of bytes in stream
Object
I/o Stream
FileInputStream
ISequenceInputStream
PipeInputStream ObjectInputStream
StringBufferedInputStrea
ByteArrayInputTream m
FileInputStream
BufferedInputStream PushBackInputSTream
DataInputSTream
DataInput
2
Output Stream Classes
Output Stream Classes are derived from the base class output stream.
Output stream is an abstract class and therefore we can not instantiate it.
The outputstream includes methods to perform the following tasks
Writing bytes
Closing stream
Flushing stream
Note: Draw the figure of Input Stream classes but change the keywords input as output
***************************************************************
3
2. Explain character stream classes?
Character Stream classes can be used to read a write 16-bit Unicode character.
There are two kinds of Character Stream Classes
Reader stream classes
Writer stream classes.
Reader Stream Classes
Reader stream classes are designed to read character form the files. Reader stream
use characters.
Object
Reader
Buffered
String Reader
Reader
CharArray Reader
PipeReader
Input FileReader
StreamReader
Filereader PushBackReader
******************************************************
4
3.Explain creation of files?
There are two ways to initializing the file stream objects. All of the constructors
require that we provide the name of the file either directly or indirectly.
Direct Approach Indirect Approach
fis “test.dat”
Deleting a file
Renaming a file
***************************************************************************
5
The two subclasses used for handling characters in files are File Reader and File Writer.
class copy
{
public static void main(String args[])
{
File infile =new (“input.dat”);
File outfile =new (“output.dat”);
ins = new FileReader(infile);
outs = new FileWriter(outfile);
int ch;
while(ch=ins.read())!=-1)
{
out.write(ch);
}
ins.close();
out.close();
}}
************************************************
************************************************************
*****************************************************
****************************************************