Monday, January 30, 2012

Writing a Disk-Oriented Program

Add the following import declarations to the beginning of your code:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
Add the following throws clause to the method header:
throws FileNotFoundException
In the call to new Scanner, replace System.in with a call to new File
as follows:
Scanner aVariableName =
new Scanner(new File(“inputFileName”))
Create a PrintStream for writing output to a disk file.
PrintStream anotherVariableName =
new PrintStream(“outputFileName”);
Use the Scanner variable name in calls to nextInt, nextLine, and so on.
For example, to go from Listing 13-1 to Listing 13-2, I change
unitPrice = myScanner.nextDouble();
quantity = myScanner.nextInt();
to
unitPrice = diskScanner.nextDouble();
quantity = diskScanner.nextInt();
Use the PrintStream variable name in calls to print and println.
For example, to go from Listing 13-1 to Listing 13-2, I change
System.out.println(total);
to
diskWriter.println(total);

No comments:

Post a Comment