Monday, January 30, 2012

Writing to a file

The print and println methods can write to disk files. Here are some
examples:

During a run of the code in Listing 13-2, the variable total stores the
number 99.75. To deposit 99.75 into the cookedData.txt file, you execute
diskWriter.println(total);
This println call writes to a disk file because of the following line in
Listing 13-2:
PrintStream diskWriter =
new PrintStream(“cookedData.txt”);
In another version of the program, you may decide not to use a total
variable. To write 99.75 to the cookedData.txt file, you can call
diskWriter.println(unitPrice * quantity);
To display OK on the screen, you can make the following method call:
System.out.print(“OK”);
Chapter 13: Piles of Files: Dealing with Information Overload 227
To write OK to a file named approval.txt, you can use the following code:
PrintStream diskWriter =
new PrintStream(“approval.txt”);
diskWriter.print(“OK”);
You may decide to write OK as two separate characters. To write to the
screen, you can make the following calls:
System.out.print(‘O’);
System.out.print(‘K’);
And to write OK to the approval.txt file, you can use the following code:
PrintStream diskWriter =
new PrintStream(“approval.txt”);
diskWriter.print(‘O’);
diskWriter.print(‘K’);
Like their counterparts for System.out, the disk-writing print and
println methods differ in their end-of-line behaviors. For example, you
want to display the following text on the screen:
Hankees Socks
To do this, you can make the following method calls:
System.out.print(“Hankees “);
System.out.println(“Socks”);
System.out.print(7);
System.out.print(“ “);
System.out.println(3);
To plant the same text into a file named scores.dat, you can use the
following code:
PrintStream diskWriter =
new PrintStream(“scores.dat”);
diskWriter.print(“Hankees “);
diskWriter.println(“Socks”);
diskWriter.print(7);
diskWriter.print(“ “);
diskWriter.println(3);

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);

A Template to Read Data from a Disk File

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
class _______________ {
public static void main(String args[])
throws FileNotFoundException {
PrintStream diskWriter =
new PrintStream(“___________”);
diskWriter.print(_____);
diskWriter.println(_____);
// Etc.
}
}
insert the name of your class into the first blank space. Type the name of the output file in the space between the quotation marks. Then, to write part of a line to the output file, call diskWriter.print. To write the remainder of a line to the output file, call diskWriter.println.

A Template to Read Data from a Disk File

Fill in the blanks. That’s all you have to do. Reading input from a disk can
work the same way

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
class _______________ {
public static void main(String args[])
throws FileNotFoundException {
Scanner diskScanner =
new Scanner(new File(“___________”));
________ = diskScanner.nextInt();
________ = diskScanner.nextDouble();
________ = diskScanner.nextLine();
________ = diskScanner.findInLine(“.”).charAt(0);
// Etc.
}
}

How to Prime a Loop

import java.util.Scanner;
class GetUserName {
public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
char symbol;
symbol = myScanner.findInLine(“.”).charAt(0);
while (symbol != ‘@’) {
System.out.print(symbol);
symbol = myScanner.findInLine(“.”).charAt(0);
}
System.out.println();
}
}

How to Prime a Loop

import java.util.Scanner;
class GetUserName {
public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
char symbol;
symbol = myScanner.findInLine(“.”).charAt(0);
while (symbol != ‘@’) {
System.out.print(symbol);
symbol = myScanner.findInLine(“.”).charAt(0);
}
System.out.println();
}
}

Trying to Get a Username from an E-mail Address

import java.util.Scanner;
class FirstAttempt {
public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
char symbol = ‘ ‘;
while (symbol != ‘@’) {
symbol = myScanner.findInLine(“.”).charAt(0);
System.out.print(symbol);
}
System.out.println();
}
}

A Simplified Version of the Game Twenty-One

import java.util.Random;
class PlayTwentyOne {
public static void main(String args[]) {
Random myRandom = new Random();
int card, total = 0;
System.out.println(“Card Total”);
while (total < 21) {
card = myRandom.nextInt(10) + 1;
total += card;
System.out.print(card);
System.out.print(“ “);
System.out.println(total);
}
if (total == 21) {
System.out.println(“You win :-)”);
} else {
System.out.println(“You lose :-(“);
}
}
}

From One File to Another Program

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
class ListOneUsername {
public static void main(String args[])
throws FileNotFoundException {
Scanner diskScanner =
new Scanner(new File(“email.txt”));
PrintStream diskWriter =
new PrintStream(“usernames.txt”);
char symbol;
symbol = diskScanner.findInLine(“.”).charAt(0);
while (symbol != ‘@’) {
diskWriter.print(symbol);
symbol = diskScanner.findInLine(“.”).charAt(0);
}
diskWriter.println();
}
}