Session-31 - Java Files and Streams
Session-31 - Java Files and Streams
Session-31 - Java Files and Streams
1
4
Files and Streams
OBJECTIVES
In this chapter you will learn:
To create, read, write and update files.
To use class File to retrieve information about files and
directories.
The Java input/output stream class hierarchy.
The differences between text files and binary files.
Sequential-access and random-access file processing.
To use classes Scanner and Formatter to process text files.
To use the FileInputStream and FileOutputStream classes.
To use a JFileChooser dialog.
To use the ObjectInputStream and ObjectOutputStream
classes.
To use class RandomAccessFile.
14.1 Introduction
14.2 Data Hierarchy
14.3 Files and Streams
14.4 Class File
14.5 Sequential-Access Text Files
14.5.1 Creating a Sequential-Access Text File
14.5.2 Reading Data from a Sequential-Access Text File
14.5.3 Case Study: A Credit-Inquiry Program
14.5.4 Updating Sequential-Access Files
14.1 Introduction
• Storage of data in variables and arrays is
temporary
• Files used for long-term retention of large
amounts of data, even after the programs that
created the data terminate
• Persistent data – exists beyond the duration of
program execution
• Files stored on secondary storage devices
• Stream – ordered data that is read from or
written to a file
FileDemonstration
RetrieveReturns
name oftrue
file orifdirectory
name is a
Returns true if name is a
file, not a directory
directory, not a file
Returns true if path was
an absolute path
Retrieve time file or directory
wasRetrieve
last modified Retrieve length of file in bytes
(system-
path entered as a string
dependent value)
FileDemonstration
Retrieve and display
contents of directory
.java
(2 of 2)
FileDemonstration
Test.java
(1 of 3)
FileDemonstration
Test.java
(2 of 3)
FileDemonstration
Test.java
(3 of 3)
AccountRecord.jav
a
(1 of 3)
AccountRecord.jav
a
(2 of 3)
AccountRecord.jav
a
(3 of 3)
.java
CreateTextFile
.java
Create AccountRecord to be
filled with user input
(2 of 4)
CreateTextFile
.java
Retrieve input,
(3 ofstore
4) data
in AccountRecord
CreateTextFile
.java
(4 of 4)
Close file
CreateTextFileTes
t
.java
(1 of 2)
CreateTextFileTes
t
.java
(2 of 2)
ReadTextFile.java
(1 of 3)
ReadTextFile.java
Create AccountRecord to
store input from file
(2 of 3)
While there is data to be read from file
Display AccountRecord
contents
ReadTextFile.java
(3 of 3)
Close file
ReadTextFileTest
.java
MenuOption.java
CreditInquiry.jav
a
(1 of 6)
CreditInquiry.jav
a
(2 of 6)
Check if record is of requested type
Retrieve input, store data
in AccountRecord
Close Scanner
CreditInquiry.jav
a
(3 of 6)
Close file
CreditInquiry.jav
a
(4 of 6)
CreditInquiry.jav
a
(5 of 6)
CreditInquiry.jav
a
Read file, display proper records
(6 of 6)
CreditInquiryTest
.java
AccountRecord
Interface Serializable specifies that
Serializable.java
AccountRecordSerializable
objects can be written to file
(1 of 3)
AccountRecord
Serializable.java
(2 of 3)
AccountRecord
Serializable.java
(3 of 3)
File.java
(1 of 4)
CreateSequential
File.java
(2 of 4)
(3 of 4)
CreateSequential
File.java
(4 of 4)
CreateSequential
FileTest.java
(1 of 3)
ReadSequentialFil
e.java
ReadSequentialFil
e.java
(3 of 3)
Close file
ReadSequentialFil
eTest.java
• RandomAccessFile class
– Includes all capabilities of FileInputStream and
FileOutputStream
– Includes capabilities for reading and writing primitive-type values,
byte arrays and strings
– Using RandomAccessFile, program can read or write data
beginning at location specified by file-position pointer
– Manipulates all data as primitive types
– Methods readInt, readDouble, readChar used to read integer,
double and character data from file
– Methods writeInt, writeDouble, writeChars used to write
integer, double and string data to file
– File-open mode – specifies whether file is opened for reading (“r”), or
for both reading and writing (“rw”). File-open mode specified as
second argument to RandomAccessFile constructor
AccountRecord.jav
a
(1 of 3)
AccountRecord.jav
a
(2 of 3)
AccountRecord.jav
a
CreateRandomFile
.java
(1 of 2)
CreateRandomFile
.java
(2 of 2)
Close file
CreateRandomFile
Test.java
WriteRandomFile
.java
(1 of 4)
WriteRandomFile
.java
(2 of 4)
WriteRandomFile
.java
(3 of 4)
.java
(4 of 4)
WriteRandomFile
Test.java
ReadRandomFile
.java
(1 of 3)
ReadRandomFile
.java
(2 of 3)
Read until non-blank record found
ReadRandomFile
.java
(3 of 3)
ReadRandomFileTes
t
.java
MenuOption.java
FileEditor.java
(1 of 5)
Close file
FileEditor.java
(2 of 5)
FileEditor.java
(3 of 5)
Retrieve record based on account number
FileEditor.java
(4 of 5)
FileEditor.java
Display record
Transaction
Processor.java
(1 of 7)
Transaction
Processor.java
(2 of 7)
Transaction
Processor.java
(3 of 7)
Transaction
Processor.java
(4 of 7)
Transaction
(5 of 7)
Delete record
Transaction
Processor.java
(6 of 7)
Transaction
Processor.java
(7 of 7)
Transaction
ProcessorTest.jav
a
FileDemonstration
(1 of 4)
FileDemonstration
.java
Create JFileChooser
Allow user to select both files and
directories (2 of 4)
Display dialog
FileDemonstration
.java
Display information about file
(3 of 4)
FileDemonstration
.java
(4 of 4)
FileDemonstration
Test.java
(1 of 2)
Select
location
for file
Click Open
here
to submit
new file
name to
program
Files and
directories
are
displayed
here
FileDemonstration
Test.java
(2 of 2)