File Processing
File Processing
Objectives:
At the end of the lesson, you will be able to:
1.Understand the concept of file processing.
2.Read data from a file
3.Apply control structures.
4.Write data to a file.
File Operations in Java
In Java, a File is an abstract data type. A named location used to store
related information is known as a File. There are several File Operations
like creating a new File, getting information about File, writing into a
File, reading from a File and deleting a File.
Stream
Byte Stream Character Stream
Next, you use the object inFile to input data from the file prog.txt
just the way you used the object console to input data from the
standard input device using the
methods next, nextInt, nextDouble, and so on.
Java file I/O process
1. Java Import necessary classes from the
packages java.util.* and java.io.* into the program
2. Create and associate appropriate objects with the
input/output sources
3. Close the files.
FileReader class
Suppose an input file, say employee.txt, consists of the following data:
FileReader
Scanner inFile = new Scanner (new FileReader("employee.txt"));
String firstName;
String lastName;
double hoursWorked;
double payRate;
double wages;
firstName = inFile.next();
lastName = inFile.next();
hoursWorked = inFile.nextDouble();
payRate = inFile.nextDouble();
wages = hoursWorked * payRate;
• This statement creates the PrintWriter object outFile and associates it with the
file employeeOutput.txt
• You can now use the methods print, println, and printf with outFile just the same
way they have been used with the object System.out
PrintWriter class..
The statement:
outFile.println("The paycheck is: " + pay);
•stores the output — The paycheck is: 607.5 — in the file employeeOutput.txt
This statement assumes that the value of the variable pay is 607.5
• In Java File I/O process requires closing the file; you close the input and output files by using the
method close
• inFile.close();
• outFile.close();
• Closing the output file ensures that the buffer holding the output will be emptied; that is, the entire
output generated by the program will be sent to the output file
throws Clause
• During program execution, various things can happen; for example, division by zero or
inputting a letter for a number
• In such cases, we say that an exception has occurred
• If an exception occurs in a method, the method should either handle the exception or
throw it for the calling environment to handle
• If an input file does not exist, the program throws a FileNotFoundException
• If an output file cannot be created or accessed, the program throws a
FileNotFoundException
• Because we do not need the method main to handle the FileNotFoundException
exception, we will include a command in the heading of the method main to throw the
FileNotFoundException exception
Input File
Sample Code
package employeeio;
import java.io.*;
import java.util.*;
public class EmployeeIO {
public static void main(String[] args) throws FileNotFoundException{
Scanner inFile = new Scanner (new FileReader ("d://emp.txt"));
PrintWriter outFile = new PrintWriter("d://empOutput.txt");
String firstName;
String lastName;
double hoursWorked;
double payRate;
double wages;
Sample Code…
while(inFile.hasNext()){
firstName = inFile.next();
lastName = inFile.next();
hoursWorked = inFile.nextDouble();
payRate = inFile.nextDouble();
wages = hoursWorked * payRate;
outFile.println(firstName + " " + lastName + " " + hoursWorked + " " + payRate );
outFile.println("The wage is: " + String.format("%.2f", wages));
outFile.println("");
}
inFile.close();
outFile.close();
}}
Output File