TCS2044 Chapter7 Input & Output Week13
TCS2044 Chapter7 Input & Output Week13
CHAPTER 7
Objectives
1. Understand and create input and output
streams.
2. Read from and write to external files using file
streams.
Stream classes
All I/Os are handled in streams
Stream classes
Java streams can be applied to any source of data.
Can be categorized in two types: byte stream and
character stream.
InputStream / OutputStream class is the root of all
byte stream.
Reader / Writer class is the root of all character
stream class.
InputStream and
Reader
The base classes for all of the input streams of
bytes and character.
InputStream is designed to read bytes.
OutputStream and
Writer
The base classes for all output streams of bytes and
character.
OutputtStream is designed to write bytes.
External Files
Processing external files
i. FileInputStream(String fileNameString);
ii. FileOutputStream(String fileNameString);
iii. FileReader(String fileNameString);
iv. FileWriter(String fileNameString);
Array Streams
Array streams
Use to read and write bytes or characters from arrays
Constructor that can be used to create array streams:
ByteArrayInputStream(byte[] byteArray);
ByteArrayOutputStream(byte[] byteArray);
ByteArrayReader(char[] byteArray);
ByteArrayWriter(char[] byteArray);
Example :
byte[] bArray = new byte[2048]
ByteArrayInputStream b = new ByteArrayInputStream
(bArray);
Filter Streams
Filte
rFilter bytes or characters for some purpose
Can use a filter class to wrap an input stream when read
integers, doubles or strings.
FilterInputStream subclasses
i. DataInputStream
- Handles binary formats of all the primitive data types.
ii. BufferedInputStream
- Gets data from the buffer and then reads it from the stream.
iii. LineNumberInputStream
- Keep track of how many lines are read.
Iv. PushbackInputStream
- Allows single-byte look ahead
MANAGEMENT & SCIENCE UNIVERSITY 8
TCS 2044 JAVA PROGRAMMING CHAPTER 7 INPUT & OUTPUT (Week 13)
FilterInputStream subclasses
i. DataOutputStream
- Output the binary format of all of the primitive types.
ii. BufferedOutputStream
- Output the buffer first and then outputs to the stream.
iii. PrintStream
- Output the Unicode format of all of the primitive types.
Data Streams
Read and write Java primitive types in a machine-independent
fashion.
DataInputStream is used to read and DataOutput is use to write.
Method defined in the DataInput interface:
readByte(), readShort(), readInt(), readLong(), readFloat(),
readDouble(), readChar(), readBoolean(), readUTF()
Method define in the DataOutput interface:
writeByte(), writeInt(), writeShort(),
Can use data I/O stream to create data streams
i. DataInputStream(InputStream istream)
ii. DataOutputStream(OutputStream outstream)
Print Streams
Use to output data into files and viwed as text.
Have two classes : PrintStream and PrintWriter
Constructor that can be used ( for PrintWriter)
PrintWriter (Writer out)
PrintWriter(Writer out)
PrintWriter(Writer out, boolean autoFlush)
PrintWriter (OutputStream out)
PrintWriter(OutputStream out, boolean autoFlush)
Example
Example