BiT 2010
Lecture 5
Event and trigger
Data Streams
01/29/2023 interpreted by : Asaminew G. 1
Event and triggers
Event: action generated by a user or a program
– Example : clicking the Play button
Event categories
– Mouse event: triggered by mouse movement or click
– Keyboard event: triggered when user presses a key
– Program event: triggered when variable value changes
– Events are triggered
Trigger is something that cause an event to take a place
01/29/2023 interpreted by : Asaminew G. 2
Data stream
Data : - file (text or binary)
Stream : - Input and Output data from/to other sources
Input can be from keyboard or a file
Output can be to display (screen) or a file
01/29/2023 interpreted by : Asaminew G. 3
Streams
Stream: an object that either delivers data to its
destination (screen, file, etc.) or that takes data from a
source (keyboard, file, etc.)
Input stream: read data from a source
–E.g. System.in is an input stream
Output stream: write data to a source
–E.g. System.out is an output stream
01/29/2023 interpreted by : Asaminew G. 4
Cont. …
…
01/29/2023 interpreted by : Asaminew G. 5
Text and Binary file
Text file :
– the bits represent printable characters
– Has BufferedReader and FileReader Input classes
– Has PrintWriter, FileOutputSteam, and FileWriter Output classes
Binary file :
– byte format data which is used by computer
– Has FileInputStream and DataInputStream input classes and
– Has FileOutputStream and DataOutputStream output classes
01/29/2023 interpreted by : Asaminew G. 6
Text file classes
…
Output classes
Input classes
01/29/2023 interpreted by : Asaminew G. 7
Binary file classes
…
Output classes
Input classes
01/29/2023 interpreted by : Asaminew G. 8
Reading and Writing Text Files
Writing to a Text File
– Writing data to a file requires three steps:
1. Connect an output stream to the file.
2. Write text data into the stream, possibly using a
loop.
3. Close the stream.
01/29/2023 interpreted by : Asaminew G. 9
Cont.…
…
01/29/2023 interpreted by : Asaminew G. 10
Cont.…
Step 2
Step 3 :- closing file outputStream.close();
01/29/2023 interpreted by : Asaminew G. 11
Example
…
01/29/2023 interpreted by : Asaminew G. 12
Cont.…
Reading from a text file : involves three steps:
Step 1 : connect input stream to the file
Step 2:
BufferedReader inputStream;
inputStream = new BufferedReader(new FileReader("StudentList.txt"));
String s = "";
s = inputStream.readLine();
while (s != null)
{
System.out.println(s);
s = inputStream.readLine();
}
01/29/2023 interpreted by : Asaminew G. 13
Step 3: close stream – inputStream.close();
Example of reading file
…
An important fact about readLine( )
is that it will return null as
its value when it reaches the
end of the file
01/29/2023 interpreted by : Asaminew G. 14
Reading and Writing Binary Files
Steps involved in reading and writing binary files are the
same as for text files:
1. Connect a stream to the file.
2. Read or write the data, possibly using a loop.
3. Close the stream.
01/29/2023 interpreted by : Asaminew G. 15
Reading binary file
Join a DataOutputStream and a FileOutputStream.
– The DataOutputStream gives us the output methods we need
– FileOutputStream lets us use the file's name to create the stream:
Syntax :
DataOutputStream outStream = new DataOutputStream(new FileOutputStream (fileName));
01/29/2023 interpreted by : Asaminew G. 16
…
Output stream is created
…
Write String
Write Integer
Write Double
01/29/2023 interpreted by : Asaminew G. 17
Reading Binary Data
checking the end-of-file marker in a binary file is
important.
Combination of DataInputStream and FileInputStream is
used.
Syntax :
DataInputStream inStream = new DataInputStream(new FileInputStream(file));
01/29/2023 interpreted by : Asaminew G. 18
…
…
read string
read integer
read double
01/29/2023 interpreted by : Asaminew G. 19
Thank you
01/29/2023 interpreted by : Asaminew G. 20