Vous êtes sur la page 1sur 31

TCS Confidential 1

Input/Output System-II
Java Programming
TCS Confidential 2
Input/Output in Java
The Character Stream Hierarchy
Data Streams
DataInputStream,
DataOutputStream
RandomAccessFile
Object Streams and Serialization
The Scanner Classes
StreamTokenizer, StringTokenizer
TCS Confidential 3
The Character Stream
Hierarchy
Object
Reader
Writer
InputStreamReader
BufferedReader
CharArrayReader
FilterReader
PipedReader
StringReader
OutputStreamWriter
FileWriter
BufferedWriter
CharArrayWriter
FilterWriter
PrintWriter
PipedWriter
StringWriter
FileReader
LineNumberReader
PushbackReader
Character Streams provide direct I/O support
for Unicodes.
TCS Confidential 4
Writing on the consol
PrintWriter is essentially a
character-oriented version of
PrintStream
PrintWriter( OutputStrem os,
boolean flushonNewln)
TCS Confidential 5
Writing on the console
PrintWriter Provides
formatted output methods -
print() and println() - for all
types including Object
print(int intValue)
print(float floatValue)
print(Object objectValue)
TCS Confidential 6
Demo: PrintWriter
This program prints different
data type values on the console
TCS Confidential 7
Reading Text Files
Reader and Writer - abstract
classes
Sub-classes:
InputStreamReader and
OutputStreamWriter - used to
work with any text
represented by ASCII
character set or Unicode
TCS Confidential 8
Reading Text Files
FileReader - subclass of
InputStreamReader, reads a
byte stream and converts the
bytes into integer values that
represent Unicode characters
read() method returns an
integer
TCS Confidential 9
Demo: Reading Text Files
Demo1: Reads one character
at a time
Demo2: Uses BufferedReader
and reads one line at a time
TCS Confidential 10
Writing Text Files
FileWriter, a subclass of
OutputStreamWriter
Convert Unicode-char-codes
to bytes
TCS Confidential 11
Writing Text Files
FileWriter w =
new FileWriter(destination.txt);
for( int j=65; j<91; j++ )
w.write((char) j);
w.close();
// Output is: ABCD XYZ
TCS Confidential 12
Writing Text Files
BufferedWriter class can be
used to write a buffered
character stream
To write a preferred EOL (End
Of Line) character of the host
platform, use newLine()
method
TCS Confidential 13
Character Stream Classes
CharArrayReader &
CharArrayWriter classes -
implementation of
input/output streams
Uses a character array as the
source or destination
TCS Confidential 14
Java IO Interfaces
DataInput
DataOutput
FilenameFilter
FileFilter
ObjectInput
ObjectOutput
ObjectInputValidation
ObjectStreamConstants
Serializable
Externalizable
TCS Confidential 15
Data Streams
When we need to work with
data that is not represented
as bytes or characters, we
can use data input and data
output streams.
TCS Confidential 16
Data Streams
These streams filter an
existing byte stream so that
each of primitive types,
boolean, byte, double, float,
int, long, and short, can be
read or written directly from
the stream
TCS Confidential 17
Data Streams ...
The data input/output streams
constructors:
DataInputStream(InputStream is)
DataOutputStream(OutputStream os)
The argument is an existing
stream that is a file or buffered
stream
TCS Confidential 18
Data Streams ...
DataInputStream implements
DataInput interface
DataOutputStream implement
DataOutput interface
These are two of the most
useful filters in java.io
package
TCS Confidential 19
Data Streams Methods
Input methods: readBoolean(),
readByte( ), readDouble(),
readFloat(), readInt(),
readLong(), readShort()
Input Methods throw both
IOException and
EOFException
TCS Confidential 20
Data Streams Methods
Output Methods:
writeBoolean() , writeByte(),
writeDouble(), writeFloat(),
writeInt(), writeLong() ,
writeShort()
Output methods throw only
the IOException
TCS Confidential 21
Demo of RWDataStream.java
This program
Finds prime numbers
Write them to a file
command line arg - using
writeInt(int value) method
Reads the number from the
file using readInt() method
TCS Confidential 22
RandomAccessFile Class
Encapsulates a random-
access file
Used to perform I/O directly at
any specific location in the file
Implements DataInput &
DataOutput interfaces
Define basic I/O methods:
getFilePointer(), length() and
seek()
TCS Confidential 23
RandomAccessFile Class
RandomAccessFile(File fileObj, String
access) throws IOException
RandomAccessFile(String filename,
String access) throws IOException
fileObj File object
fileName name of the file
access r or rw
TCS Confidential 24
Demo: RandomAccessFile
The program
Creates a random access file
Writes some data to it
Reads it and displays it on
the console
TCS Confidential 25
Object Streams
The Object Streams are used
to read and write complete
objects from and to a stream
ObjectInputStream and
ObjectOutputStream classes
are used to perform object
serialization
TCS Confidential 26
Object Streams
The classes which implement
the interface Serializable can
be serialized
serialver tool is used to find
out that a class is serializable
or not
TCS Confidential 27
Object Streams
There are two methods which
help in performing object I/O
public abstract Object readObject()
throws ClassNotFoundException,
IOExcepion
public abstract void
writeObject(Object obj) throws
IOExcepion
TCS Confidential 28
Demo: Serializable
The demo program
Creates an object of Book
class which is serializable
Writes the Book object to a file
Reads the Book object from
the file and assigns to a Book
class variable and displays its
values
TCS Confidential 29
The Scanner Classes
StreamTokenizer filter breaks
up a stream of characters into
a stream of tokens
If the stream of characters is
a sentence, the tokens
represent the words and
punctuation marks that make
up the sentence
TCS Confidential 30
The Scanner Classes
A non-IO class that can also
provide a scanner is
StringTokenizer which
implements the Enumeration
interface. The individual
tokens contained in a string
can be enumerated using
StringTokenizer
TCS Confidential 31
Summary
The Java I/O System
RandomAccessFile Classes
The Byte and Character
Stream Hierarchies
The Data Streams
The Object Streams
The Scanner Classes -
StreamTokenizer,
StringTokenizer

Vous aimerez peut-être aussi