0% found this document useful (0 votes)
55 views

EEE 5 DDA Lab 9 File Input and Output in Java 1 1

This document discusses file input and output in Java. It covers using the Path and Files classes to gather file information and perform operations. It describes organizing data into fields, records, and files and using input/output streams and buffers. It discusses writing to and reading from both sequential and random access files, and creating empty files with default records. Finally, it presents exercises to create multiple random access files based on customer state, write a method to create an empty file, add data entry capability, and set up a program to read the created files.

Uploaded by

Twing Siacor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

EEE 5 DDA Lab 9 File Input and Output in Java 1 1

This document discusses file input and output in Java. It covers using the Path and Files classes to gather file information and perform operations. It describes organizing data into fields, records, and files and using input/output streams and buffers. It discusses writing to and reading from both sequential and random access files, and creating empty files with default records. Finally, it presents exercises to create multiple random access files based on customer state, write a method to create an empty file, add data entry capability, and set up a program to read the created files.

Uploaded by

Twing Siacor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Lab 9

File Input and output in Java

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

A brief Introduction to Java File Input and Output


In Java, Data items can be stored on two broad types of storage devices - temporary,
volatile storage, or permanent, nonvolatile storage. A computer file is a collection of data
stored on a nonvolatile device. Files can be text files or binary files, but all files share
characteristics, such as a size, name, and time of creation.
Java’s Path class is used to gather file information, such as its location, size, and creation
date. You can use the Files class to perform operations on files and directories, such as
deleting them, determining their attributes, and creating input and output streams.
Businesses organize data in a hierarchy of character, field, record, and file. When a
program performs input and output operations, bytes flow into a program stream, which
functions as a pipeline or channel. A buffer is a memory location where bytes are held after
they are logically output but before they are sent to the output device. Using a buffer to
accumulate input or output improves program performance. Flushing clears any bytes that
have been sent to a buffer for output but that have not yet been output to a hardware
device.
InputStream, OutputStream, and Reader are subclasses of the Object class that are used
for input and output. Output devices can be assigned to OutputStream references, allowing
applications to save data to them. You can create a file and write to it by using the Files
class newOutputStream() method. To open a file for reading, you can use the
newInputStream() method.
The BufferedWriter class contains write() methods that are used to create data files. Files
can be read using the BufferedReader class. The String class split() method accepts an
argument that identifies a field delimiter and returns an array of Strings in which each array
element holds one field.
Businesses store data in sequential order when they use the records for batch processing.
Real-time applications require interactive processing with random access files. Java’s
Lab Workbook/Lab 9 File Input and Output in Java

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.

File Input and Output in Java


2
Lab Workbook/Lab 9 OOP Concepts

Stavros Dimitriou
3
Lab Workbook/Lab 9 File Input and Output in Java

Show and explain the results in your logbook.

Exercise 2: Writing a Method to Create an Empty File

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.

File Input and Output in Java


4
Lab Workbook/Lab 9 OOP Concepts

Show and explain the results in your logbook.

Exercise 3: Adding Data-Entry Capability to the Program

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

File Input and Output in Java


6
Lab Workbook/Lab 9 OOP Concepts

Stavros Dimitriou
7
Lab Workbook/Lab 9 File Input and Output in Java

Figure 13-41 Typical execution of the CreateFilesBasedOnState program

Figure 13-42 Part of the contents of the files created by the program execution in Figure
13-41

File Input and Output in Java


8
Lab Workbook/Lab 9 OOP Concepts

Show and explain the results in your logbook.

Exercise 4: Setting Up a Program to Read the Created Files

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

Show and explain the results in your logbook.

Exercise 5: Displaying File Statistics

In the next section of the program, you display the creation time and size of the file.

File Input and Output in Java


10
Lab Workbook/Lab 9 OOP Concepts

Stavros Dimitriou
11
Lab Workbook/Lab 9 File Input and Output in Java

File Input and Output in Java


12
Lab Workbook/Lab 9 OOP Concepts

Figure 13-43 Typical execution of the ReadStateFile program

Stavros Dimitriou
13

You might also like