Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Outline
17.1 Introduction
17.2 Data Hierarchy
17.3 Files and Streams
17.4 Class File
17.5 Creating a Sequential-Access File
17.6 Reading Data from a Sequential-Access File
17.7 Updating Sequential-Access Files
17.8 Random-Access Files
17.9 Creating a Random-Access File
17.10 Writing Data Randomly to a Random-Access File
17.11 Reading Data Sequentially from a Random-Access File
17.12 Case Study: A Transaction-Processing Program
17.13 New I/O APIs for the Java Platform
• Files
– Long-term storage of large amounts of data
– Persistent data exists after termination of program
– Files stored on secondary storage devices
• Magnetic disks
• Optical disks
• Magnetic tapes
– Sequential and random access files
• Data hierarchy
– Data items in a computer form a hierarchy
• Progresses from bits, to characters, to fields, etc.
• Records
– Composed of several fields
– Implemented as a class in Java
– See Fig. 17.1 for example
• File is a group of related records
– One field in each record is a record key
• Record key is a unique identifier for a record
– Sequential file
• Records stored in order by record key
Sally Black
Tom Blue
Judy Green
File
Iris Orange
Randy Red
Judy Green
Record
Judy Field
1 Bit
...
0 1 2 3 4 5 6 7 8 9 n-1
...
end-of-file marker
• Buffering
– Improves performance of I/O
– Copies each output to a region of memory called a buffer
– Entire buffer output to disk at once
• One long disk access takes less time than many smaller ones
– BufferedOutputStream buffers file output
– BufferedInputStream buffers file input
• Class File
– Provides useful information about a file or directory
– Does not open files or process files
• Fig. 17.3 lists some useful File methods
boolean isFile() Returns true if the name specified as the argument to the File constructor is a
file; false otherwise.
boolean isDirectory() Returns true if the name specified as the argument to the File constructor is a
directory; false otherwise.
boolean isAbsolute() Returns true if the arguments specified to the File constructor indicate an
absolute path to a file or directory; false otherwise.
String getAbsolutePath() Returns a string with the absolute path of the file or directory.
String getName() Returns a string with the name of the file or directory.
String getPath() Returns a string with the path of the file or directory.
String getParent() Returns a string with the parent directory of the file or directory—that is, the
directory in which the file or directory can be found.
long length() Returns the length of the file, in bytes. If the File object represents a directory,
0 is returned.
long lastModified() Returns a platform-dependent representation of the time at which the file or
directory was last modified. The value returned is useful only for comparison
with other values returned by this method.
String[] list() Returns an array of strings representing the contents of a directory. Returns null
if the File object is not a directory.
FileTest.java
• Java Files
– Java imposes no structure on a file
– Programmer structures file according to application
– Following program uses simple record structure
CreateSequentia
lFile.java
Select location
for file here
Files and
directories are Click Save to
displayed here submit new
file name to
program
Sample Data
100 Bob Jones 24.98
200 Steve Doe -345.67
300 Pam White 0.00
400 Sam Stone -42.16
500 Sue Rich 224.62
ReadSequentialF
ile.java
CreditInquiry.j
ava
• “Instant-access” applications
– Record must be located immediately
– Transaction-processing systems require rapid access
• Random-access files
– Access individual records directly and quickly
– Use fixed length for every record
• Easy to calculate record locations
– Insert records without destroying other data in file
– Fig. 16.10 shows random-access file
byte offsets
• RandomAccessFile objects
– Like DataInputStream and DataOutputstream
– Reads or writes data in spot specified by file-position pointer
• Manipulates all data as primitive types
• Normally writes one object at a time to file
CreateRandomFil
e.java
WriteRandomFile
.java
ReadRandomFile.
java
• Buffers
– Consolidate I/O operations
– Four properties
• Capacity
• Limit
• Position
• Mark
– Put and get operations
• Relative or absolute
– Clear, flip, rewind, reset
• Channels
– Connection to an I/O device
• Interacts efficiently with buffers
– ReadableByteChannel interface
• Method read
– WriteableByteChannel interface
• Method write
– Scattering reads and gather writes
– Class FileChannel
• File Locks
– Restricts access to a portion of a file
– FileChannel, position, size
– Exclusive or shared
• Charsets
– Package java.nio.charset
• Class Charset
– Methods decode, encode
• Class CharsetDecoder, CharsetEncoder