UNIT-5: Managing Input / Output in JAVA

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 58

UNIT-5

Managing Input / Output in JAVA


, Other Stream Classes.
Introduction
• We can overcome the problem of loss of data by storing
data on secondary storage devices such as floppy disks or
hard disks. The data stored in these devices using the
concept of files. Data stored in files is often called
persistent data
• A file is a collection of related records placed in a
particular area on the disk. A record is composed of
several fields and a field is a group of characters.
Characters in Java are Unicode characters composed of
two bytes, each byte containing eight binary digits ,1 or 0
• Storing and managing data using files is known as file
processing which includes tasks such as creating files,
updating files and manipulation of data
Data representation in Java files
Concept of Streams
• In file processing, input refers to the flow of
data into a program and output means the
flow of data out of a program
• Input to a program may come from the
keyboard, the mouse, the memory, the disk, a
network , or another program . Similarly,
output from a program may go to the screen,
the printer, the memory, the disk, a network,
or another program
Relationship of Java program with I/O devices
• Java uses the concept of streams to represent the
ordered sequence of data, a common
characteristic shared by all the input/output
devices
• A stream presents a uniform, easy-to-use,object-
oriented interface between the program and the
input/output devices
• A stream in Java is a path along which data flows
.It has a source (of data) and a destination(for that
data) as shown in figure below. Both the source
and destination may be physical devices or
programs or other streams in the same program
Conceptual view of a stream
• Java streams are classified into two basic
types,namely,input stream and output stream
• An input stream extracts (reads) data from the
source (file) and sends it to the
program.Similarly, an output stream takes
data from the program and sends (writes) it to
the destination(file)
Fig p 296
Stream Classes
• The java.io package contains a large number of
stream classes that provide capabilities for
processing all types of data
• These classes are categorized into two groups
based on the data type on which they operate
1. Byte stream classes that provide support for
handling I/O operations on bytes
2. Character stream classes that provide support for
managing I/O operations on characters
3. Figure below shows how stream classes are
grouped based on their functions
Byte Stream Classes
• Byte stream classes have been designed to
provide functional features for creating and
manipulating streams and files for reading and
writing bytes
• Since the streams are unidirectional, they can
transmit bytes in only one direction and ,
therefore, java provides two kinds of byte
stream classes :
1. Input stream classes
2. Output stream classes
Input Stream Classes
• Input stream classes that are used to read 8-bit
bytes include a super class known as InputStream
and a number of subclasses for supporting various
input-related functions
• Fig below shows the class hierarchy of input
stream classes
• The super class InputStream is an abstract
class,therefore,we cannot create instances of this
class, we must use the subclasses that inherit from
this class
Summary of InputStream methods
Method Description

read( ) Reads a byte from input stream

read(byte b[ ]) Reads an array of bytes into b

read(byte b[ ],int n,int m) Reads m bytes into b starting from nth byte

available( ) Gives number of bytes available in the input

skip(n) Skips over n bytes from the input stream

reset() Goes back to the beginning of the stream

Close( ) Closes the input stream


Output Stream Classes
• Output stream classes are derived from the
base class OutputStream as shown in fig
below
• Like InputStream, the OutputStream is an
abstract class and therefore we cannot
instantiate it
• Several subclasses of the OutputStream can
be used for performing the output operations
Summary of OutputStream methods
Method Description
write( ) Writes a byte to the output stream
write(byte b[ ]) Writes all bytes in the array b to the output stream

write(byte b[ ],int n,int m) Writes m bytes from array b starting from nth byte

close( ) Closes the output stream


flush( ) Flushes the output stream
Character Stream Classes
• Character stream classes were not a part of
the language when it was released in
1995.They were added later when version 1.1
was announced
• Character streams can be used to read and
write 16-bit Unicode characters
• There are two kinds of character stream
classes , namely , reader stream classes and
writer stream classes
Reader stream classes
• Reader stream classes are designed to read
character from the files
• Reader class is the base class for all other
classes in this group as shown in fig below
• These classes are functionally very similar to
the input stream classes, except input streams
use bytes as their fundamental unit of
information, while reader streams use
characters
Writer Stream Classes
• Like output stream classes, the writer stream
classes are designed to perform all output
operations on files
• The writer stream classes are designed to
write characters
• The writer class is an abstract class which acts
as a base class for all the other writer stream
classes as shown in fig below
Using Streams
• Although all the classes are known as I/O classes,
not all of them are used for reading and writing
operations only. Some perform operations such as
buffering,filtering,data conversion, counting and
concatenation while carrying out I/O tasks
• Both the character stream group and the byte
stream group contain pairs of classes that perform
the same kind of operation but for different data
type
• Table below gives a list of tasks and the character
streams and byte streams that are available to
implement them
Task Character Stream class Byte Stream class

Performing input operations Reader InputStream

Buffering input BufferedReader BufferedInputStream

Reading from an array CharArrayReader ByteArrayInputStream

Reading from files FileReader FileInputStream

Reading from a string StringReader StringBufferInputStream

Buffering output BufferedWriter BufferedOutputStream

Writing to a file FileWriter FileOutputStream

Writing to a string String Writer (none)


Other Useful I/O Classes
• The java.io package supports many other classes
for performing certain specialized functions. They
include
1. RandomAccessFile
2. StreamTokenizer
• The RandomAccessFile enables us to read and
write bytes, text and java data types to any
location in a file
• The class Stream Tokenizer , a subclass of object
can be used for breaking up a stream of text from
an input text file into meaningful pieces called
tokens
Using the File Class
• The java.io package includes a class known as the File class
that provides support for creating files and directories
• The class includes several constructors for instantiating
the File objects. The class also contains several methods
for supporting the operations such as
 Creating a file
 Opening a file
 Closing a file
 Deleting a file
 Getting the name of a file
 Renaming a file
 Getting the size of a file
 Checking whether the file is writable
 Checking whether the file is readable
Input/output Exceptions
• When creating files and performing I/O
operations on them, the system may generate
I/O related exceptions
• The basic I/O related exception classes and
their functions are given in below table
Important I/O exception classes and their
functions
I/O Exception class Function
EOFException Signals that an end of file or end of stream has been
reached unexpectedly during input
FileNotFoundException Informs that a file could not be found
InterruptedIOException Warns that an I/O operations has been interrupted
IOException Signals that an I/O exception of some sort has
occured
• Each I/O statement or group of I/O statements must
have an exception handler around it as shown below
try
{
..............
............... //I/O statements
}
catch (IOException e)
{
................ //message output statement
}
• Proper use of exception handlers would help us
identify and locate I/O errors more effectively
Creation of Files
• If one wants to create and use a disk file, we need to
decide the following about the file
 Suitable name for the file
 Data type to be stored
 Purpose(reading , writing or updating)
 Method of creating the file
• A filename is a unique string of characters that helps
identify a file on the disk. The length of filename and
characters allowed are dependent on the OS on
which Java program is executed
• Data type is important to decide the type of file
stream classes to be used for handling the data
• The purpose of using a file must also be
decided before using it. i.e. Reading, writing or
both
• For using a file,it must be opened first.This is
done by creating a file stream and then linking
it to the filename
• A file stream can be definedusing the classes
of Reader/InputStream for reading data and
Writer/OutputStream for writing data
Common stream classes used for I/O operations

Source or Characters Characters Bytes Bytes


Destinatio Write Read Write
n Read
Memory CharArrayReader CharArrayWriter ByteArrayInputStr ByteArrayoutputSt
eam ream
File FileReader FileWriter FileInputStream FileOutputStream

Pipe PipedReader PipedWriter PipedInputStream PipedOutputStrea


m
• There are two ways of initializing the file stream
objects. All of the constructors require that we provide
the name of the file either directly or indirectly
• Direct approach
FileInputStream fis //Declare a file stream object
try
{
//Assign the filename to the file stream object
fis=new FileInputStream (“test.dat”);
..........................
}
catch (IOException e)
....................
....................
• Indirect approach uses a file object that has been initialized with
the desired filename,which is illustrated by the following code
..............................
..............................
File inFile; //Declare a file object
inFile= new File (“test.dat”); //assign the
// filename to the file object
FileInputStream fis;
try
{
//give the value of the file object to the file stream object
fis = new FileInputStream (inFile);
...................................
}
catch (...............)
.....................
....................
• The code above includes five tasks:
1. Select a filename
2. Declare a file object
3. Give the selected name to the file object
declared
4. Declare a file stream object
5. Connect the file to the file stream object
Reading/Writing Characters
• Subclasses of Reader and Writer implement
streams that can handle characters
• The two subclasses used for handling
characters in files are FileReader and
FileWriter
• Following program uses two file stream
classes to copy contents of a file named
“infile.txt” into a file called “outfile.txt”
class CopyChar
{ public static void main(String[] args)
{
File infile = new File("infile.txt");
File outfile = new File("Outfile.txt");
FileReader fr=null;
FileWriter fw=null;
Try
{
fr = new FileReader(infile);
fw = new FileWriter(outfile);
int ch;
//FileReader Class which is subclass of InputSteamReader
//InputStreamReader class converts Strings or character data
//into Byte which are in form of integers.
//so ch is of int type
while((ch=fr.read()) != -1)
{
//-1 indicates end-of-file (eof)
fw.write(ch);
}
}catch(IOException e)
{
System.out.println(e);
System.exit(-1);
}
finally
{
try
{
fr.close();
fw.close();
}
catch(IOException e) { }
}
}
}
• Above program creates two file objects infile and
outfile and initializes them with “infile.txt” and
“outfile.txt” respectively using code
File infile = new File("infile.txt");
File outfile = new File("Outfile.txt");
• Then program creates two file stream objects fr and
fw and initializes them to “null” as shown below
FileReader fr=null;
FileWriter fw=null;
• These streams are connected to the named files
using the following code:
fr = new FileReader(infile);
fw = new FileWriter(outfile);
Reading/Writing Bytes
• FileReader and FileWriter classes are used to
read and write 16-bit characters.However
most file systems use only 8-bit bytes.Java i/o
system provides a number of classes that can
handle 8-bit bytes
• Two commonly used classes for handling bytes
are FileInputStream and FileOutputStream
classes
import java.io.*;
class WritingBytes
{
public static void main(String[] args)
{
//Declare and initialize a byte array
byte cities[ ] = {'D‘ , 'E‘ , 'L‘ , 'H‘ , 'I‘ , '\n', 'M‘ , 'A‘ , 'D‘ , 'R‘ , 'A‘ , 'S‘ , '\n‘ , 'L‘ , 'O‘ , 'N‘ , 'D‘ , 'O‘ , 'N‘ ,
'\n'};
//Create an output file stream
FileOutputStream fos =null;
try
{
fos= new FileOutputStream("city.txt");
fos.write(cities);
fos.close();
}
catch(IOException e)
{
System.out.println (ioe);
System.exit( -1 );
}
}
}
Handling Primitive Data Types
• The basic input and output streams provide read/write
methods that can only be used for reading/writing bytes
or characters
• If we want to read/write the primitive data types such as
integers and doubles, we can use filter classes as wrappers
on existing input and output streams to filter data in the
original stream
• The two filter classes used for creating “ data streams” for
handling primitive types are DataInputStream and
DataOutStream
• These classes use the concept of multiple inheritance and
therefore implements all the methods contained in both
the parent class and the interface
• A data stream for input can be created as follows:
FileInputStream fis = new FileInputStream (infile);
DataInputStream dis=new DataInputStream (fis);
• These statements first create the input file stream
fis and then create the input data stream dis. These
statements basically wrap dis on fis and use it as a
“filter”.Similarly, the following statements create
the output data stream dos and wrap it over the
output file stream fos
FileOutputStream fos = new FileOutputStream
(outfile);
DataOutputStream dos=new DataOutputStream (fos);
• Note that the file objects infile and outfile
must be initialized with appropriate file names
before they are used
• Program below demonstrates the use of data
streams for reading and writing primitive data
types
import java.io.*;
class ReadWritePrimitive
{
publicstaticvoid main(String[] args)
{
Try
{
FileOutputStream fos = new FileOutputStream("data.txt");
DataOutputStream dos = new DataOutputStream(fos);
//Writing primitive data to the data.txt file
dos.writeInt(2000);
dos.writeDouble(888.88);
dos.writeChar('V');
dos.writeBoolean(false);
fos.close();
dos.close();
//Reading primitive data to the data.txt
file FileInputStream fis = new FileInputStream("data.txt");
DataInputStream dis = new DataInputStream(fis);
System.out.println(dis.readInt());
System.out.println(dis.readDouble());
System.out.println(dis.readChar());
System.out.println(dis.readBoolean());
fis.close();
dis.close();
}
}
Concatenating and Buffering Files
• It is possible to combine two or more input streams (files)
into a single input stream(file).This process is known as
concatenation of files and is achieved using the
SequenceInputStream class
• One of the constructors of this class takes two InputStream
objects as arguments and combines them to construct a
single input stream
• Java also supports creation of buffers to store temporarily
data that is read from or written to to a stream.The process
is known as buffered I/O operation.A buffer sits between
the program and the source(or destination) and functions
like a filter. Buffers can be created using the
BufferedInputStream and BufferedOutputStream classes
import java.io.*;
class SequenceBuffer
{
publicstaticvoid main(String[] args)
{
Try
{
FileInputStream file1 = new FileInputStream("dataFile1.txt");
FileInputStream file2 = new FileInputStream("dataFile2.txt");
SequenceInputStream resultFile = null;
resultFile = new SequenceInputStream(file1,file2);
BufferedInputStream inBuffer = new BufferedInputStream(resultFile);
BufferedOutputStream outBuffer = new
BufferedOutputStream(System.out);
int ch;
while((ch = inBuffer.read())!=1)
{
outBuffer.write((char)ch);
}
inBuffer.close();
outBuffer.close(); f
ile1.close();
file2.close();
catch(Exception e)
{
}
}
}
Interactive Input and Output
• Data is generated for writing to files within the
program or used data from other files stored in the
memory
• What if the data is to be provided through the
keyboard ?This can be done in Java by using an
object of the DataInputStream class
• The process of reading data from the keyboard and
displaying output on the screen is known as
interactive I/O ,which are of two types
1. Simple interactive I/O
2. Graphical interactive I/O
Simple interactive I/O

• The System class contains three I/O objects, namely System.in ,


System.out and System.err where in, out and err are static
variables
• The variable in is of InputStream type and the other two are of
PrintStream type. We can use these objects to input from the
keyboard, output to the screen and display error messages
• System class is stored in java.lang package which is imported
into a java program automatically
• To perform keyboard input for primitive data types,we need to
use the objects of DataInputStream and String Tokenizer classes
• The following code illustrates the reading of an integer value
from the keyboard
static DataInputStream din=new
DataInputStream(System.in);
Static String Tokenizer st;
.....................................
.....................................
st = new StringTokenizer (din.readLine);
Int code = Integer.parseint (st.nextToken);
Graphical Input and Output
• Various classes can be used for graphical input
and output
• The TextField class can be used to create text
fields that receive information from the user at
the keyboard and then writes the information
to a file
• Other classes such as Frame,TextField,Button
and Label of java.awt package can be used to
create the window and the text fields required
to receive data
Other Stream Classes
• Java supports many other I/O streams That
are useful in some situations
1. Object Streams
2. Piped Streams
3. Pushback Streams
4. Filtered Streams
• Object Streams We can read and write
characters,bytes and primitive data types.It is also
possible to perform input and output operations on
objects using the object streams.The object streams
are created using the ObjectInputStream and
ObjectOutput Stream classes
• Piped StreamsPiped streams provide functionality
for threads to communicate and exchange data
between them.Two threads use pipes for
communication.The write thread sends data to the
read thread through a pipeline that connects an
object of PipedInputStream to an object of
PipedOutputStream
• Pushback Streams The pushback streams created
by the classes PushbackInputStream and
PushbackReader can be used to push a single byte
or a character back into the input stream so that it
can be reread. This is commonly with parsers
• Filtered Streams Java supports two abstract
classes, namely, FilterInputStream and
FilterOutputStream that provide the basic
capability to create input and output streams for
filtering I/O in a number of waysThese
streams,known as filters, sit between an input
stream and an output stream

You might also like