0% found this document useful (0 votes)
51 views19 pages

File Processing

This document discusses file processing in Java. It covers reading from and writing to files, using classes like File, FileReader, PrintWriter, and Scanner. The key points are: 1. Files are used to store related information and there are operations to create, read, write, and delete files. 2. Streams are used for file input/output - byte streams for binary data and character streams for text. 3. The FileReader class is used to read from files and Scanner to input data from files. 4. The PrintWriter class is used to write output to files. 5. Sample code is provided to read employee data from a file, calculate wages, and write

Uploaded by

KioGaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views19 pages

File Processing

This document discusses file processing in Java. It covers reading from and writing to files, using classes like File, FileReader, PrintWriter, and Scanner. The key points are: 1. Files are used to store related information and there are operations to create, read, write, and delete files. 2. Streams are used for file input/output - byte streams for binary data and character streams for text. 3. The FileReader class is used to read from files and Scanner to input data from files. 4. The PrintWriter class is used to write output to files. 5. Sample code is provided to read employee data from a file, calculate wages, and write

Uploaded by

KioGaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

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

Byte Stream is mainly Character Stream is


involved with byte data. A mainly involved with
file handling process with character data. A file
handling process with a
a byte stream is a process
character stream is a
in which an input is process in which an input
provided and executed is provided and executed
with the byte data. with the character data.
File
• is an area in a secondary storage to hold information.
• You can also initialize a Scanner object to input sources other than the standard input
device by passing an appropriate argument in place of the object System.in
• We make use of the class FileReader
• Suppose that the input data is stored in a file, say prog.txt, and this file is on the drive
C:
• The following statement creates the Scanner object inFile and initializes it to the
file prog.txt
Scanner inFile = new Scanner (new FileReader(" prog.txt”));
• This statement assumes that the file prog.txt is in the same directory (subdirectory) as
your program. However, if this is in different directory(subdirectory), then you must specify
the path where the file is located, along with the name of the file.
Example:

Suppose that the file prog.txt is on a flash memory in drive E.


Then, the statement above should be modified as follows:
Scanner inFile = new Scanner
(new FileReader("e://prog.txt”));

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;

inFile.close(); //close the input file


PrintWriter class
• To store the output of a program in a file, you use the class PrintWriter
• Declare a PrintWriter variable and associate this variable with the destination
• Suppose the output is to be stored in the file employeeOutput.txt
• Consider the following statement:

PrintWriter outFile = new PrintWriter("d://employeeOutput.txt");

• 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

You might also like