Download The data hierarchy The data hierarchy (contd.) Record key

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
8/17/2011
File processing
2
sequential files
Lesson 11
CSC 108 3.0
OBJECT ORIENTED
PROGRAMMING
Ms. GS Makalanda
File Processing
The data hierarchy
3
The data hierarchy (contd.)
4
Bit
Characters
Fields
The smallest data item
A file
Byte – composed of eight bits
Any source or destination of information
A group of related records
Database
A group of related files
Composed of characters (bytes)
Record
A group of related fields
File Processing
File Processing
Record key
Organizing records in file
5
6
To facilitate the retrieval of specific records from a
file, at least one field in each record is chosen
Record key
Tow types
It identifies a record as belonging to a particular
entity that is distinct from all other records in the file
Sequential files
Random access files
Also
called a direct access file
Registration number of a student
File Processing
File Processing
1
8/17/2011
Sequential files
Random access files
7
8
The Values only can be accessed in the same
sequence in which they are stored
To process we must move through successive data
items in the same order as their respective locations
on the storage device
Values can be accessed in any order desired by the
programmer
File Processing
File Processing
Files and Streams
Text files
9
10
Java views each file as a sequential stream of
bytes.
end-of file marker to determine the end of a file.
File streams can be used to input and output data
as
Characters
Bytes
Character
Files created using character based streams
Can be read by text editors
based streams
Byte-based streams
File Processing
File Processing
Binary files
Streams and devices
11
12
Created using byte-based streams
Read by a program that converts the data to a
human-readable format.
Java also can associate streams with different
devices.
Three stream objects are asssociated with devices
when a Java program begins executing
System.in
System.out
System.err
File Processing
File Processing
2
8/17/2011
java.io package
Classes Scanner and Formatter
13
14
Includes definitions for stream classes
FileInputStream
FileOutputStream
For
For
FileWriter
Can be used for character based I/O
Class Scanner is used to input data from the
keyboard
byte-based output to a file
FileReader
For
byte-based input from file
For
character-based input from a file
Can also read data from a file
Class Formatter enables formatted data to be
output to the screen.
Can output to a file similar to System.out.printf
character-based output to a file
File Processing
File Processing
Class File
File Handling
15
16
Class File is used to obtain information about files and
directories.
Objects of Class File do not open files or provide any
file-processing capabilities.
File objects are used frequently with objects of other
java.io classes to specify files or directories.
The constructor
Open the file
Process the file
Read from the file
Write to the file
Close the file
public File(String name)
specifies the name of a file or directory to associate
with the File object.
File Processing
File Processing
Opening a file
Opening a file
17
18
A java program opens a file by creating an object
of stream classes and associating a stream of bytes
or characters with it.
Declare the object used to input/output text to file
Input
Output
Private
Private
Open the file
Input
Output
input
= new Scanner(new File("myData.txt"));
output
= new Formatter("myData.txt");
Scanner input;
Formatter output;
File Processing
File Processing
3
8/17/2011
Read/Write data
Close file
19
20
Reading using Scanner
Value = input.nextInt();
input.close();
output.close();
Writing data
Output.format("%s %d\n", name, age);
File Processing
File Processing
End of file
21
A system-dependent keystroke combination which
the user enters to indicate that there is no more
data
The Scanner method hasNext determines whether
there is more data to input.
Returns true if there is more data
Otherwise it returns false.
File Processing
4