• Data input/output activities (inputting data from the
keyboard, reading data from a file, writing data to the screen, writing to a file, writing to disk, printing to a printer...) are all referred to as streams. • All streams operate on the same principle even when they are associated with different physical devices.
Spring 2024 61FIT3PR2 – Programming 2 2
Introduction of Data Streams • Input stream is a stream that allows the program to read data from a source such as keyboard, file, scanner...
Spring 2024 61FIT3PR2 – Programming 2 3
Introduction of Data Streams • Output stream is a stream that allows the program to write data to it to be sent to a destination such as screen, file, printer...
Spring 2024 61FIT3PR2 – Programming 2 4
Inheritance hierarchy of Input Streams
Spring 2024 61FIT3PR2 – Programming 2 5
Inheritance hierarchy of Output Streams
Spring 2024 61FIT3PR2 – Programming 2 6
Byte streams • Using byte streams for binary data input/output • All byte streams inherit from 2 classes: • InputStream • OutputStream • There are many byte stream classes: • FileInputStream • FileOutputStream • They differ in initialization methods but operate in the same way. Spring 2024 61FIT3PR2 – Programming 2 7 FileInputStream/FileOutputStream
• This stream is used to work with binary files.
• Use FileInputStream to read data from binary files. • Use FileOutputStream to write data to binary files.
Spring 2024 61FIT3PR2 – Programming 2 8
FileOutputStream • FileOutputStream is an output stream used to write data to binary files.
Spring 2024 61FIT3PR2 – Programming 2 9
FileInputStream • FileInputStream is the input stream for reading data from binary files.
Spring 2024 61FIT3PR2 – Programming 2 10
DataInputStream/DataOutputStream • These two streams help us read/write primitive data. • Use read<Type>() to read primitive data from DataInputStream. • readInt() • readDouble()… • Use write<Type>(Type) to write primitive data to DataOutputStream. • writeBoolean(boolean) • writeInt(int)…
Spring 2024 61FIT3PR2 – Programming 2 11
DataOutputStream
Spring 2024 61FIT3PR2 – Programming 2 12
DataInputStream
Spring 2024 61FIT3PR2 – Programming 2 13
ObjectInputStream/ObjectOutputStream
• This stream helps us read/write objects.
• Use readObject() to read objects from DataInputStream. • Use writeObject(Serializable) to write objects to DataOutputStream. • Note: • Only objects created from classes implementing the Serializable interface can be read/written.
Spring 2024 61FIT3PR2 – Programming 2 14
interface Serializable
Spring 2024 61FIT3PR2 – Programming 2 15
ObjectOutputStream
Spring 2024 61FIT3PR2 – Programming 2 16
ObjectInputStream
Spring 2024 61FIT3PR2 – Programming 2 17
Character Streams • Byte streams are powerful and flexible. However, if you want to store files containing Unicode text, character streams are the best choice because the advantage of character streams is that they operate directly on Unicode characters. • All character streams are inherited from 2 abstract classes: Reader and Writer.
Spring 2024 61FIT3PR2 – Programming 2 18
FileWriter/ FileReader
Spring 2024 61FIT3PR2 – Programming 2 19
BufferedReader/ BufferedWriter
• This stream is used to work with buffered
character streams. • Use BufferedReader to work with buffered input text streams. • Use BufferedWriter to work with buffered output text streams.
Spring 2024 61FIT3PR2 – Programming 2 20
BufferedWriter
Spring 2024 61FIT3PR2 – Programming 2 21
BufferedReader
Spring 2024 61FIT3PR2 – Programming 2 22
Using Try… Catch in Input/Output
• When input/outputting data, there are 'checked'
exceptions that must be caught when writing code, usually these exceptions are: • FileNotFoundException • EOFException • NotSerializableException • IOException
Spring 2024 61FIT3PR2 – Programming 2 23
Using Try… Catch in Input/Output
Spring 2024 61FIT3PR2 – Programming 2 24
Summary • Data Streams • Inheritance hierarchy of Input Streams/ Output Streams • Byte streams • FileInputStream/FileOutputStream • DataInputStream/DataOutputStream • ObjectInputStream/ObjectOutputStream • FileWriter/ FileReader • BufferedReader/ BufferedWriter • Using Try… Catch in Input/Output