EEE 5 DDA Lab 9 File Input and Output in Java 1 1
EEE 5 DDA Lab 9 File Input and Output in Java 1 1
Learning Objectives
Upon completion of this chapter, you will be able to:
• Describe computer files
• Use the Path and Files classes
• Describe file organization, streams, and buffers
• Use Java’s IO classes to write to and read from a file
• Create and use sequential data files
• Appreciate using random access files
• Write records to a random access data file
• Read records from a random access data file
FileChannel class creates random access files. A file channel is seekable, meaning you
can search for a specific file location and operations can start at any specified position.
One approach to writing a random file is to place records into the file based on a key field
that makes a record unique from all others. The first step in creating the random access
file is to create a file that holds default records. Then you can replace any default record
with actual data by setting the file channel position.
You can process a random access file either sequentially or randomly. The benefit of using
a random access file is the ability to retrieve a specific record from a file directly, without
reading through other records to locate the desired one.
Lab Work
Exercise 1: Creating Multiple Random Access Files
In this exercise, you write a class that prompts the user for customer data and assigns the
data to one of two files depending on the customer’s state of residence. This program
assumes that Wisconsin (WI) records are assigned to an in-state file and that all other
records are assigned to an out-of-state file. First, you will create empty files to store the
records, and then you will write the code that places each record in the correct file.
Stavros Dimitriou
3
Lab Workbook/Lab 9 File Input and Output in Java
In this exercise, you write the method that creates empty files using the default record
format string. The method will create 1,000 records with an account number of 000.
In these steps, you add the code that accepts data from the keyboard and writes it to the
correct location (based on the customer’s account number) within the correct file (based
on the customer’s state).
Stavros Dimitriou
5
Lab Workbook/Lab 9 File Input and Output in Java
Stavros Dimitriou
7
Lab Workbook/Lab 9 File Input and Output in Java
Figure 13-42 Part of the contents of the files created by the program execution in Figure
13-41
Now, you can write a program that can use either of the files you just created. The program
has four parts:
• The program will prompt the user to enter the filename to be used and set up all
necessary variables and constants.
• A few statistics about the file will be displayed.
• The nondefault contents of the file will be displayed sequentially.
• A selected record from the file will be accessed directly.
Stavros Dimitriou
9
Lab Workbook/Lab 9 File Input and Output in Java
In the next section of the program, you display the creation time and size of the file.
Stavros Dimitriou
11
Lab Workbook/Lab 9 File Input and Output in Java
Stavros Dimitriou
13