FILE PROCESSING
Understanding Computer Files
• Temporary storage
– Computer memory or random access memory (RAM)
– Volatile
• Permanent storage
– Not lost when computer loses power
– Nonvolatile
• Computer file
– Collection of information stored on nonvolatile device in computer system
• Work with stored files in application
– Determine whether and where file exists
– Open file
– Read data from file
– Write information to file
– Close file
Open a file and read data from a file
A file is a collection of records. Each record is a collection of fields.
To process a file, the following packages must import in a Java program.
import java.io.*;
import java.util.*;
Use the Java classes File and BufferReader to open a file and
read data from a file named student.txt.
Below is the content of file student.txt
Duckey Donald 85
Goof Goofy 89
Brave Balto 93
Snow White 93
Alice Wonderful 89
Sleeping Beauty 85
Simba Lion 95
Dumbo Elephant 90
Brown Deer 86
Johny Jocky 95
Grump Grumpy 75
Hap Happy 80
Dop Dopey 80
Bash Bashful 85
Sleep Sleepy 70
Sneeze Sneezey 83
Shelly Malik 95
Chelsea Tomek 95
Angela Clodfelter 95
Allison Nields 95
The file student.txt has three fields: last name, first name and
test score.
Problem: Read all the student records from the file student.txt and
display the last name, first name and test score for each student on the output.
Click here to down load the student.txt file.
Solution:
//Open a file named student.txt which is located in the directory CIS 103 under
the C drive.
FileReader fr = new FileReader("c:\\cecs174/student.txt");
// fr is a file object. Use fr in the coding to reference to the file
student.txt
Note: if the file student.txt is located in the C drive
FileReader fr = new FileReader("c:\\student.txt");
//Create a BufferReader object to store data read from the file
student.txt.
BufferedReader br = new BufferedReader( fr ); // br is the
BufferReader object.
// Read the first record ( Duckey Donald 85) and store it in a
string object called inputLine.
inputLine = br.readLine(); // br is the BufferedReader object. readLine is
a method which reads one record at at time. inputLine is a string object defined
as
// String inputLine.
//Each record in the student.txt has three fields: last name, first name and
test score. There are twenty records in the file. You want to read from the file
as part of a while loop until you read EOF (end of file).
//When using the readLine( ) method as part of a loop, you know
when you reach EOF because the readLine( ) method returns a null value
when EOF is reached.
int i = 0; // i is an index variable
inputLine = br.readLine();
while(inputLine != null)
{
tokenizer = new StringTokenizer(inputLine);
Last[i] = tokenizer.nextToken();
First[i] = tokenizer.nextToken();
score[i] = Integer.parseInt(tokenizer.nextToken());
inputLine = br.readLine();
i++;
}//end while loop
Note:
inputLine = br.readLine // Read the first record ( Duckey Donald 85) and store it in a string object called inputLine.
while the current inputLine object is not null (not EOF)
Create a StringTokenizer object called tokenizer
(user-defined name)
tokenizer = new StringTokenizer(inputLine);
StringTokenizer will extract three fields (last name, first name and
score) from the inputLine object and store them in the tokens (string
data type). To extract the first field (last name), use the method
nextToken( ).Store the last name in an array called Last[i] where i is
an integer index variable.
Last[i] = tokenizer.nextToken();
Read the the next token (the second field: first name)
First[i] = tokenizer.nextToken();
Read the next token (test score. Since test score is a
numeric data type - integer), you need to use Integer.parseInt to
convert the string score to integer data type.
score[i] = Integer.parseInt(tokenizer.nextToken());
Read the next record
inputLine = br.readLine();
Increase the index by one
i++; or i = i + 1;
Where Last, First and score are one-dimensional array.
Display all the records whose last name, first name and score are store in the arrays Last, Fist and score by using a for loop or while loop.
for(i =0; i < 19; i++)
System.out.println(First[i]+", "+Last[i]+ " " +score[i]);
A Complete Java program
import javax.swing.*;
import java.io.*;
import java.util.*;
public class Test
{public static void main(String[] args) throws FileNotFoundException, IOException
{ String Last[]= new String[20];
String First[] = new String[20];
String inputLine="";
StringTokenizer tokenizer;
int score[]=new int[20];
int i = 0;
FileReader fr = new FileReader("c:\\cis103grade/student.txt");
BufferedReader br = new BufferedReader(fr);
inputLine = br.readLine();
while(inputLine != null)
{
tokenizer = new StringTokenizer(inputLine);
Last[i] = tokenizer.nextToken();
First[i] = tokenizer.nextToken();
score[i] = Integer.parseInt(tokenizer.nextToken());
inputLine = br.readLine();
i++;
}//end while loop
for(i =0; i < 20; i++)
System.out.println(First[i]+", "+Last[i]+ " " +score[i]);
System.exit(0);
}
}
Output
Donald, Duckey 85
Goofy, Goof 89
Balto, Brave 93
White, Snow 93
Wonderful, Alice 89
Beauty, Sleeping 85
Lion, Simba 95
Elephant, Dumbo 90
Deer, Brown 86
Jocky, Johny 95
Grumpy, Grump 75
Happy, Hap 80
Dopey, Dop 80
Bashful, Bash 85
Sleepy, Sleep 70
Sneezey, Sneeze 83
Malik, Shelly 95
Tomek, Chelsea 95
Clodfelter, Angela 95
Nields, Allison 95