CS111: PROGRAMMING LANGUAGE II … · Files and Streams Java views each file as a sequential stream...
Transcript of CS111: PROGRAMMING LANGUAGE II … · Files and Streams Java views each file as a sequential stream...
![Page 1: CS111: PROGRAMMING LANGUAGE II … · Files and Streams Java views each file as a sequential stream of bytes. Every operating system provides a mechanism to determine the end of a](https://reader033.fdocuments.in/reader033/viewer/2022052804/604ba3cffea27829b1468aa7/html5/thumbnails/1.jpg)
CS111: PROGRAMMING
LANGUAGE II
Lecture 13(a): Introduction to Streams Computer Science
Department
![Page 2: CS111: PROGRAMMING LANGUAGE II … · Files and Streams Java views each file as a sequential stream of bytes. Every operating system provides a mechanism to determine the end of a](https://reader033.fdocuments.in/reader033/viewer/2022052804/604ba3cffea27829b1468aa7/html5/thumbnails/2.jpg)
Lecture Contents
dr. Amal Khalifa, 2014
Data Hierarchy
Files and streams
Sequential text files
File open
Writing data
File close
2
![Page 3: CS111: PROGRAMMING LANGUAGE II … · Files and Streams Java views each file as a sequential stream of bytes. Every operating system provides a mechanism to determine the end of a](https://reader033.fdocuments.in/reader033/viewer/2022052804/604ba3cffea27829b1468aa7/html5/thumbnails/3.jpg)
Introduction to File Processing
Data stored in variables and arrays is temporary.
For long-term retention of data, even after the programs
that create the data terminate, computers use files.
Computers store files on secondary storage devices such as
hard disks, optical disks, flash drives and magnetic tapes.
Data maintained in files is persistent data because it exists
beyond the duration of program execution.
File processing is a subset of Java’s stream-processing
capabilities, which include reading from and writing to
memory, files and network connections.
dr. Amal Khalifa, 2014
3
![Page 4: CS111: PROGRAMMING LANGUAGE II … · Files and Streams Java views each file as a sequential stream of bytes. Every operating system provides a mechanism to determine the end of a](https://reader033.fdocuments.in/reader033/viewer/2022052804/604ba3cffea27829b1468aa7/html5/thumbnails/4.jpg)
Data Hierarchy
Bit: binary digit
Byte : 8 bits
Character : Unicode
Field : group of characters
Record: group of fields
File : group of related records
dr. Amal Khalifa, 2014
4
![Page 5: CS111: PROGRAMMING LANGUAGE II … · Files and Streams Java views each file as a sequential stream of bytes. Every operating system provides a mechanism to determine the end of a](https://reader033.fdocuments.in/reader033/viewer/2022052804/604ba3cffea27829b1468aa7/html5/thumbnails/5.jpg)
dr. Amal Khalifa, 2014 5
![Page 6: CS111: PROGRAMMING LANGUAGE II … · Files and Streams Java views each file as a sequential stream of bytes. Every operating system provides a mechanism to determine the end of a](https://reader033.fdocuments.in/reader033/viewer/2022052804/604ba3cffea27829b1468aa7/html5/thumbnails/6.jpg)
Data Hierarchy (cont.)
To facilitate the retrieval of specific records from a
file, at least one field in each record is often chosen
as a record key.
A record key identifies a record as belonging to a
particular person or entity and is unique to each
record.
This field typically is used to search and sort records.
In a sequential file, records are stored in order by
the record-key field.
dr. Amal Khalifa, 2014
6
![Page 7: CS111: PROGRAMMING LANGUAGE II … · Files and Streams Java views each file as a sequential stream of bytes. Every operating system provides a mechanism to determine the end of a](https://reader033.fdocuments.in/reader033/viewer/2022052804/604ba3cffea27829b1468aa7/html5/thumbnails/7.jpg)
Files and Streams
Java views each file as a sequential stream of bytes.
Every operating system provides a mechanism to determine the end of a file (end-of-file marker )
A Java program receives an indication from the operating system when it reaches the end of the stream
dr. Amal Khalifa, 2014
7
![Page 8: CS111: PROGRAMMING LANGUAGE II … · Files and Streams Java views each file as a sequential stream of bytes. Every operating system provides a mechanism to determine the end of a](https://reader033.fdocuments.in/reader033/viewer/2022052804/604ba3cffea27829b1468aa7/html5/thumbnails/8.jpg)
Files and Streams
Streams that input and output characters
representing data as a sequence of characters.
Files created text files read by text editors.
Streams that input and output bytes
representing data in its binary format.
Files created binary files read by programs that understand the content of the file.
8
dr. Amal Khalifa, 2014
character-based streams byte-based streams
![Page 9: CS111: PROGRAMMING LANGUAGE II … · Files and Streams Java views each file as a sequential stream of bytes. Every operating system provides a mechanism to determine the end of a](https://reader033.fdocuments.in/reader033/viewer/2022052804/604ba3cffea27829b1468aa7/html5/thumbnails/9.jpg)
Files and Streams in Java
A Java program opens a file by creating an object and associating a stream of bytes or characters with it.
creates three stream objects in Java :
System.in (input stream object)
normally enables a program to input bytes from the keyboard
System.out (output stream object)
normally enables a program to output character data to the screen.
System.err (error stream object) normally enables a program to output character-based error messages to the screen.
dr. Amal Khalifa, 2014
9
![Page 10: CS111: PROGRAMMING LANGUAGE II … · Files and Streams Java views each file as a sequential stream of bytes. Every operating system provides a mechanism to determine the end of a](https://reader033.fdocuments.in/reader033/viewer/2022052804/604ba3cffea27829b1468aa7/html5/thumbnails/10.jpg)
IO Exception Handling
dr. Amal Khalifa, 2014
10
When you programmatically interact with files on
disk, there are many potential problems that could
occur.
Security restrictions on the computer could prevent the
program from opening a file.
If there is something wrong with the disk drive, the
computer could have trouble writing information into
the file.
A program that reads from a file could attempt to open
a file that does not exist, and so on.
![Page 11: CS111: PROGRAMMING LANGUAGE II … · Files and Streams Java views each file as a sequential stream of bytes. Every operating system provides a mechanism to determine the end of a](https://reader033.fdocuments.in/reader033/viewer/2022052804/604ba3cffea27829b1468aa7/html5/thumbnails/11.jpg)
IO Exception Handling
IO exceptions are checked exceptions:
To satisfy the catch part of the catch-or-declare requirement, the
code that generates the exception must be wrapped in a try
block and must provide a catch handler for the checked-
exception type (or one of its superclasses).
To satisfy the declare part of the catch-or-declare requirement, the method must provide a throws clause containing the
checked-exception type after its parameter list and before its
method body.
If the catch-or-declare requirement is not satisfied, the compiler
will issue an error message indicating that the exception must be
caught or declared.
dr. Amal Khalifa, 2014
11
![Page 12: CS111: PROGRAMMING LANGUAGE II … · Files and Streams Java views each file as a sequential stream of bytes. Every operating system provides a mechanism to determine the end of a](https://reader033.fdocuments.in/reader033/viewer/2022052804/604ba3cffea27829b1468aa7/html5/thumbnails/12.jpg)
How to store this data in a file?
Case study 12
dr. Amal Khalifa, 2014
![Page 13: CS111: PROGRAMMING LANGUAGE II … · Files and Streams Java views each file as a sequential stream of bytes. Every operating system provides a mechanism to determine the end of a](https://reader033.fdocuments.in/reader033/viewer/2022052804/604ba3cffea27829b1468aa7/html5/thumbnails/13.jpg)
The Student class
dr. Amal Khalifa, 2014
13
Data
Constructor
set methods
![Page 14: CS111: PROGRAMMING LANGUAGE II … · Files and Streams Java views each file as a sequential stream of bytes. Every operating system provides a mechanism to determine the end of a](https://reader033.fdocuments.in/reader033/viewer/2022052804/604ba3cffea27829b1468aa7/html5/thumbnails/14.jpg)
The Student class (cont.)
dr. Amal Khalifa, 2014
14
get methods
And…
toString
![Page 15: CS111: PROGRAMMING LANGUAGE II … · Files and Streams Java views each file as a sequential stream of bytes. Every operating system provides a mechanism to determine the end of a](https://reader033.fdocuments.in/reader033/viewer/2022052804/604ba3cffea27829b1468aa7/html5/thumbnails/15.jpg)
Case Study: data
dr. Amal Khalifa, 2014
15
Store the following Students data on a file :
Name SSN DoB UnvId Faculty Level
Malak Hamad 12345 12-3-1997 22222 Arts 2
Maryam Nabil 34567 10-2-1998 33333 Medicine 1
Salma Hamdy 47689 3-5-1995 44444 Computer
Science
3
![Page 16: CS111: PROGRAMMING LANGUAGE II … · Files and Streams Java views each file as a sequential stream of bytes. Every operating system provides a mechanism to determine the end of a](https://reader033.fdocuments.in/reader033/viewer/2022052804/604ba3cffea27829b1468aa7/html5/thumbnails/16.jpg)
Creating Text Files
Formatter
outputs
formatted
Strings to the
screen or a
file
dr. Amal Khalifa, 2014
16
![Page 17: CS111: PROGRAMMING LANGUAGE II … · Files and Streams Java views each file as a sequential stream of bytes. Every operating system provides a mechanism to determine the end of a](https://reader033.fdocuments.in/reader033/viewer/2022052804/604ba3cffea27829b1468aa7/html5/thumbnails/17.jpg)
Creating Text Files
If the file does
not exist, it
will be
created.
If an existing
file is opened,
its contents
are truncated
dr. Amal Khalifa, 2014
17
![Page 18: CS111: PROGRAMMING LANGUAGE II … · Files and Streams Java views each file as a sequential stream of bytes. Every operating system provides a mechanism to determine the end of a](https://reader033.fdocuments.in/reader033/viewer/2022052804/604ba3cffea27829b1468aa7/html5/thumbnails/18.jpg)
dr. Amal Khalifa, 2014
18
Java
imposes no
structure on
a file
Formatter
object
method
format
a Formatter
object must
be
eventually
closed
![Page 19: CS111: PROGRAMMING LANGUAGE II … · Files and Streams Java views each file as a sequential stream of bytes. Every operating system provides a mechanism to determine the end of a](https://reader033.fdocuments.in/reader033/viewer/2022052804/604ba3cffea27829b1468aa7/html5/thumbnails/19.jpg)
File processing
dr. Amal Khalifa, 2014
19
Open Write Close
![Page 20: CS111: PROGRAMMING LANGUAGE II … · Files and Streams Java views each file as a sequential stream of bytes. Every operating system provides a mechanism to determine the end of a](https://reader033.fdocuments.in/reader033/viewer/2022052804/604ba3cffea27829b1468aa7/html5/thumbnails/20.jpg)
Main!!
dr. Amal Khalifa, 2014
20
Three
records
added
Each on a
separate
line
![Page 21: CS111: PROGRAMMING LANGUAGE II … · Files and Streams Java views each file as a sequential stream of bytes. Every operating system provides a mechanism to determine the end of a](https://reader033.fdocuments.in/reader033/viewer/2022052804/604ba3cffea27829b1468aa7/html5/thumbnails/21.jpg)
dr. Amal Khalifa, 2014 21
![Page 22: CS111: PROGRAMMING LANGUAGE II … · Files and Streams Java views each file as a sequential stream of bytes. Every operating system provides a mechanism to determine the end of a](https://reader033.fdocuments.in/reader033/viewer/2022052804/604ba3cffea27829b1468aa7/html5/thumbnails/22.jpg)
Working on chapter 17…
That’s all for today….. 22