No Slide Title - WordPress.com

Download Report

Transcript No Slide Title - WordPress.com

The I/O Package
1
Objectives
After completing this chapter, the student will be able to:
•
•
•
•
•
•
•
Explain Java’s stream model of data input and output
Explain various input stream classes and their class
hierarchy
Write simple Java programs to take data from the
keyboard
Explain various output stream classes and their class
hierarchy
Write Java programs to input and output data using
different stream classes
Create files and write data into them
Read data from an existing file
2
Introduction
•
Input data can come from the keyboard, from files
stored on external devices, from information stored in
RAM memory or from the output of another program.
•
A program can output information to the screen, to a
printer, to a file stored on an external device or into
the input of another program.
•
The Java I/O package provides an extensive set of
classes for handling input and output from/to any of
the sources mentioned above.
3
Java Input Stream Classes
InputStream class
FileInputStream class
ByteArrayInputStream class
StringBufferInputStream class
SequenceInputStream class
PipedInputStream class
FilterInputStream class
BufferedInputSteram class
LineNumberInputStream class
PushBackInputStream class
DataInputStream class
The java input stream classes and their class hierarchy.
4
Java Input Stream Classes contd..
InputStream Class
•
•
The Input stream class is an abstract class that
serves as a base for all other input classes.
The InputStream class has the following member
functions:
• abstract int read()
• int read(byte b[])
• int read(byte b[] , int offset, int length)
• long skip(long n)
• int available()
• synchronized void mark(int readlimit)
• synchronized void reset()
• boolean markSupported()
• void close()
5
Java Input Stream Classes contd..
FileInputStream Class
•
•
The FileInputStream class is useful for performing
simple file input operations.
The FileInputStream class has the following
constructors:
• public FileInputStream(String name) throws
FileNotFoundException
• public FileInputStream(File file) throws
FileNotFoundException
• public
FileInputStream(FileDescriptor
fdobj)
throws
FileNoteFoundException
6
Java Input Stream Classes contd..
ByteArrayInputStream Class
•
This class implements a buffer that can be used as
input stream.
•
All though the byte array is in memory, this stream
allows you to access it via read() calls.
•
The ByteArrayInputStream class has the following
constructors :
• public ByteArrayInputStream (byte buffer[])
• public ByteArrayInputStream (byte buffer[], int
offset, int length)
7
Java Input Stream Classes contd..
StringBufferInputStream Class
•
StringBufferInputStream class allows to use a string
as buffered source of input similar to the
ByteArrayInputStream class.
•
The StringBufferInputStream class has the following
member variables:
• String buffer;
• int count;
• int pos;
8
Java Input Stream Classes contd..
SequenceInputStream Class
•
SequenceInputStream class takes a series of input
streams and effectively concatenate them.
•
It allowes the programmer to access them as though
they were just one long stream.
•
SequenceInputStream class has two constructors :
• public SequenceInputStream(Enumeration e)
• public SequenceInputStream (InputStream s1,
InputStream s2)
9
Java Input Stream Classes contd..
PipedInputStream Class
•
This class is used in pairs with PipedOutputStream
class.
•
It allows two threads to communicate using I/O calls.
•
What one thread writes into the PipedOutputStream
will be read by the other thread that has instantiated
PipedInputStream.
10
Java Input Stream Classes contd..
FilterInputStream Class
•
This is an abstract class derived from InputStream
class.
•
It provides the ability to stack several input streams
on top of each other.
11
Java Input Stream Classes contd..
BufferedInputStream Class
•
The BufferedInputStream class provides a buffered
stream of input.
•
Subsequent reads come from the buffer rather than
from the input device.
•
The BufferedInputStream class has the following
member variables.
• byte buffer[];
• int count;
• int pos;
• int markpos;
12
Java Input Stream Classes contd..
LineNumberInputStream Class
•
This class keeps track of the number of lines that has
been read.
•
It also provides the ability to set and retrieve the line
count.
PushBackInputStream Class
•
This class provides a method to push back a byte
that has been read back into the input stream.
13
Java Input Stream Classes contd..
DataInputStream Class
•
The DataInputStream class is used for reading
primitive data types from an input stream.
•
This class has methods for all the built-in data types:
boolean, int, float etc.
•
These are written out as binary data and is used in
the same way.
•
The DataInputStream class has only one constructor:
DataInputStream(InputStream in)
14
Java Output Stream Classes
•
In Java, the output stream classes are the logical
counter parts of the input stream classes.
•
It handle the writing of data to the output source.
15
Java Output Stream Classes contd..
OutputStream Class
•
The OutputStream class is the output counterpart of
the InputStream class.
•
This serves as an abstract base class for the other
output stream classes.
•
The OutputStream class implements the following
member functions:
• abstract void write(int b)
• void write(byte b[])
• void write(byte b[], int offset, int length)
• void flush()
• void close()
16
Java Output Stream Classes contd..
FileOutputStream Class
•
The FileOutputStream class provides a means to
perform simple file output.
•
FileOutputStream class has three constructors:
• FileOutputStream(String name)
• FileOutputStream(File file)
• FileOutputStream(FileDescriptor fdobj)
•
This class defines the following set of member
functions:
• public void write(byte b[]) throws IOExceptions
• public native void close() throws IOException
• protected void finalize() throws IOException
17
Java Output Stream Classes contd..
ByteArrayOutputStream Class
•
The ByteArrayOutputStream class implements an inmemory byte array that can be written to as an
output stream.
•
The ByteArrayOutputStream class has
constructors :
• public ByteArrayOutputStream()
• public ByteArrayOutputStream(int size)
two
18
Java Output Stream Classes contd..
PipedOutputStream Class
•
PipedOutputStream class is used in conjunction with
a PipedInputStream to let threads pass data to each
other by means of writes and reads on a common
pipe.
•
PipedOutputStream class has two constructors:
• public
PipedOutputStream(PipedInputStream
snk)
throws IOException
• public PipedOutputStream()
19
Java Output Stream Classes contd..
FilterOutputStream Class
•
This is an abstract class derived from OutputStream
class.
•
It supports an ‘output’ stream of bytes that is filtered
in some way before being passed on for output.
•
The next three output classes are derived, from this
FilterOutputStream class.
20
Java Output Stream Classes contd..
BufferedOutputStream Class
•
BufferedOutputStream class maintains a buffer that
is written to when you write to the stream.
•
When the buffer gets full or when it is explicitly
flushed it is written to the output device.
•
The BufferedOutputStream class has two member
variables that manages the buffer itself:
• byte bufer[];
• int count
21
Java Output Stream Classes contd..
The PrintStream Class
•
The PrintStream class is designed primarily for
printing output data as text.
•
The PrintStream class has two constructors:
• PrintStream(OutputStream out)
• PrintStream(OutputStream
out,
boolean
autoflush)
•
The PrintStream class provides a variety of print
methods to handle all your printing needs.
22
Java Output Stream Classes contd..
DataOutputStream Class
•
DataOutputStream class is useful for writing primary
data type onto an output stream.
•
The DataOutputStream class has only
constructor:
• DataOutputStream(OutputStream out)
one
23
The File Class
•
The File class models an operating system directory
entry, enabling you to access information about a file:
file attributes, file path etc.
•
The File class has three constructors:
• File(String path)
• File(String path, String name)
• File(File dir, String name)
24
The RandomAccessFile Class
•
RandomAccessFile class provides a number of
methods for reading and writing files.
•
RandomAccessFile class has two constructors:
• RandomAccessFile(String name, String mode)
• RandomAccessFile (File file, String mode)
•
The RandomAccessFile class implements a variety
of powerful member functions :
• int skipBytes(int n)
• long getFilePrinter()
• void seek (long pos) etc.
25