0% found this document useful (0 votes)
52 views

TCS2044 Chapter7 Input & Output Week13

This document discusses input and output streams in Java. It covers the different stream classes for byte and character input/output, including file streams to read from and write to external files. It also describes filter streams that can transform input streams, data streams that allow reading and writing Java primitive types, and print streams for outputting data to files as text. The objectives are to understand and create input/output streams and read from and write to external files using file streams.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

TCS2044 Chapter7 Input & Output Week13

This document discusses input and output streams in Java. It covers the different stream classes for byte and character input/output, including file streams to read from and write to external files. It also describes filter streams that can transform input streams, data streams that allow reading and writing Java primitive types, and print streams for outputting data to files as text. The objectives are to understand and create input/output streams and read from and write to external files using file streams.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

TCS 2044 JAVA PROGRAMMING CHAPTER 7 INPUT & OUTPUT (Week 13)

CHAPTER 7

INPUT & OUTPUT

MANAGEMENT & SCIENCE UNIVERSITY 1


TCS 2044 JAVA PROGRAMMING CHAPTER 7 INPUT & OUTPUT (Week 13)

Objectives
1. Understand and create input and output
streams.
2. Read from and write to external files using file
streams.

MANAGEMENT & SCIENCE UNIVERSITY 2


TCS 2044 JAVA PROGRAMMING CHAPTER 7 INPUT & OUTPUT (Week 13)

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.

MANAGEMENT & SCIENCE UNIVERSITY 3


TCS 2044 JAVA PROGRAMMING CHAPTER 7 INPUT & OUTPUT (Week 13)

InputStream and
Reader
The base classes for all of the input streams of
bytes and character.
InputStream is designed to read bytes.

Reader is designed to read characters.

MANAGEMENT & SCIENCE UNIVERSITY 4


TCS 2044 JAVA PROGRAMMING CHAPTER 7 INPUT & OUTPUT (Week 13)

OutputStream and
Writer
The base classes for all output streams of bytes and
character.
OutputtStream is designed to write bytes.

Writer is designed to write characters

MANAGEMENT & SCIENCE UNIVERSITY 5


TCS 2044 JAVA PROGRAMMING CHAPTER 7 INPUT & OUTPUT (Week 13)

External Files
Processing external files

Use file streams to read from or write to a disk file.


Byte streams : FileInputStream or FileOutputStream

Character streams : FileReader or FileWriter

Constructor that can be used to create a file stream:

i. FileInputStream(String fileNameString);
ii. FileOutputStream(String fileNameString);
iii. FileReader(String fileNameString);
iv. FileWriter(String fileNameString);

MANAGEMENT & SCIENCE UNIVERSITY 6


TCS 2044 JAVA PROGRAMMING CHAPTER 7 INPUT & OUTPUT (Week 13)

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);

MANAGEMENT & SCIENCE UNIVERSITY 7


TCS 2044 JAVA PROGRAMMING CHAPTER 7 INPUT & OUTPUT (Week 13)

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.

MANAGEMENT & SCIENCE UNIVERSITY 9


TCS 2044 JAVA PROGRAMMING CHAPTER 7 INPUT & OUTPUT (Week 13)

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)

MANAGEMENT & SCIENCE UNIVERSITY 10


TCS 2044 JAVA PROGRAMMING CHAPTER 7 INPUT & OUTPUT (Week 13)

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)

MANAGEMENT & SCIENCE UNIVERSITY 11


TCS 2044 JAVA PROGRAMMING CHAPTER 7 INPUT & OUTPUT (Week 13)

Example

MANAGEMENT & SCIENCE UNIVERSITY 12


TCS 2044 JAVA PROGRAMMING CHAPTER 7 INPUT & OUTPUT (Week 13)

Example

MANAGEMENT & SCIENCE UNIVERSITY 13

You might also like