cs111: programming language ii … · files and streams java views each file as a sequential stream...
TRANSCRIPT
CS111: PROGRAMMING
LANGUAGE II
Lecture 13(a): Introduction to Streams Computer Science
Department
Lecture Contents
dr. Amal Khalifa, 2014
Data Hierarchy
Files and streams
Sequential text files
File open
Writing data
File close
2
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
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
dr. Amal Khalifa, 2014 5
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
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
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
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
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.
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
How to store this data in a file?
Case study 12
dr. Amal Khalifa, 2014
The Student class
dr. Amal Khalifa, 2014
13
Data
Constructor
set methods
The Student class (cont.)
dr. Amal Khalifa, 2014
14
get methods
And…
toString
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
Creating Text Files
Formatter
outputs
formatted
Strings to the
screen or a
file
dr. Amal Khalifa, 2014
16
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
dr. Amal Khalifa, 2014
18
Java
imposes no
structure on
a file
Formatter
object
method
format
a Formatter
object must
be
eventually
closed
File processing
dr. Amal Khalifa, 2014
19
Open Write Close
Main!!
dr. Amal Khalifa, 2014
20
Three
records
added
Each on a
separate
line
dr. Amal Khalifa, 2014 21
Working on chapter 17…
That’s all for today….. 22