TCS Confidential 1
Input/Output System-II
Java Programming
TCS Confidential 2
Input/Output in Java The Character Stream Hierarchy Data StreamsDataInputStream,
DataOutputStreamRandomAccessFileObject Streams and Serialization The Scanner ClassesStreamTokenizer, StringTokenizer
TCS Confidential 3
The Character Stream Hierarchy
ObjectReader WriterInputStreamReaderBufferedReaderCharArrayReaderFilterReaderPipedReaderStringReader
OutputStreamWriterFileWriter
BufferedWriterCharArrayWriter
FilterWriterPrintWriter
PipedWriterStringWriter
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: PrintWriterThis program prints different
data type values on the console
TCS Confidential 7
Reading Text FilesReader and Writer - abstract
classesSub-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 FilesDemo1: Reads one character
at a time
Demo2: Uses BufferedReaderand reads one line at a time
TCS Confidential 10
Writing Text FilesFileWriter, a subclass of
OutputStreamWriterConvert Unicode-char-codes
to bytes
TCS Confidential 11
Writing Text Files FileWriter w =new FileWriter(destination.txt);for( int j=65; j
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 StreamsWhen 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 StreamsThese 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 interfaceDataOutputStream implement
DataOutput interface These are two of the most
useful filters in java.iopackage
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 MethodsOutput Methods:
writeBoolean() , writeByte(),writeDouble(), writeFloat(),writeInt(), writeLong() ,writeShort()Output methods throw only
the IOException
TCS Confidential 21
Demo of RWDataStream.javaThis programFinds prime numbersWrite them to a file
command line arg - using writeInt(int value) methodReads the number from the
file using readInt() method
TCS Confidential 22
RandomAccessFile ClassEncapsulates a random-
access fileUsed to perform I/O directly at
any specific location in the file Implements DataInput &
DataOutput interfacesDefine basic I/O methods:
getFilePointer(), length() and seek()
TCS Confidential 23
RandomAccessFile Class RandomAccessFile(File fileObj, String
access) throws IOExceptionRandomAccessFile(String filename,
String access) throws IOException
fileObj File object fileName name of the fileaccess r or rw
TCS Confidential 24
Demo: RandomAccessFileThe programCreates a random access fileWrites some data to it Reads it and displays it on
the console
TCS Confidential 25
Object StreamsThe Object Streams are used
to read and write complete objects from and to a streamObjectInputStream 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 serializableor 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: SerializableThe demo programCreates an object of Book
class which is serializableWrites the Book object to a fileReads 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
SummaryThe Java I/O SystemRandomAccessFile ClassesThe Byte and Character
Stream HierarchiesThe Data StreamsThe Object StreamsThe Scanner Classes -
StreamTokenizer, StringTokenizer