docx

advertisement
Practice Problems: File Reading and Writing
1. Tracing code with file operations
Suppose I have a directory on my disk that contains two files called "input.txt" and "output.txt".
Before each of the classes below executes, assume that the files start off with the following
contents, written as a stream of chars:
input.txt
output.txt
This planet has — or rather had — a problem,
which was this: most of the people living on it were
unhappy for pretty much all of the time. \nMany
solutions were suggested for this problem, but most
of these were largely concerned with the movement
of small green pieces of paper, which was odd
because on the whole it wasn't the small green
pieces of paper that were unhappy.\nMany were
increasingly of the opinion that they'd all made a
big mistake in coming down from the trees in the
first place.\nAnd some said that even the trees had
been a bad move, and that no one should ever
have left the oceans.
In many of the more relaxed civilizations on the
Outer Eastern Rim of the Galaxy,
the <i>Hitchhiker's Guide</i> has already
supplanted the great <i>Encyclopaedia
Galactica</i> as the standard repository of all
knowledge and wisdom, for though it has many
omissions and contains much that is apocryphal, or
at least wildly inaccurate, it scores over the older,
more pedestrian work in two important
respects.\nFirst, it is slightly cheaper; and secondly
it has the words <b>DON'T PANIC</b> inscribed in
large friendly letters on its cover.\n
For each of the following programs, show what memory looks like at the end of the code, AND
show what is displayed on the screen (if anything), AND show what the files contain at the end
of the code.
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Files-Initialization {
public static void main(String [] args) throws FileNotFoundException {
File input = new File("input.txt");
Scanner fileScanner = new Scanner(input);
String token = fileScanner.next();
String line = fileScanner.nextLine();
fileScanner.close(); // If you open it, close it!
}
}
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Files-BasicReadingCommands {
public static void main(String [] args) throws FileNotFoundException {
File input = new File("input.txt");
Scanner fileScanner = new Scanner(input);
System.out.println(fileScanner.next());
String token = fileScanner.next();
String line1 = fileScanner.nextLine();
String line2 = fileScanner.nextLine();
System.out.println(line2);
fileScanner.close();
}
}
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Files-HasNext {
public static void main(String [] args) throws FileNotFoundException {
File input = new File("input.txt");
Scanner fileScanner = new Scanner(input);
int c = 0, cNum = 0, sum = 0;
String s;
while(fileScanner.hasNext()) {
if(fileScanner.hasNextInt()) {
sum = sum + fileScanner.nextInt();
cNum++;
} else {
s = fileScanner.next();
}
c++;
}
System.out.println(c);
System.out.println(cNum);
fileScanner.close();
}
}
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Files-HasNextLine {
public static void main(String [] args) throws FileNotFoundException {
File input = new File("input.txt");
Scanner fileScanner = new Scanner(input);
int c = 0;
while(fileScanner.hasNextLine()) {
fileScanner.nextLine();
c++;
}
System.out.println(c);
fileScanner.close();
}
}
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class Files-BasicFileWriting {
public static void main(String [] args) throws FileNotFoundException {
File input = new File("input.txt");
File output = new File("output.txt");
Scanner fileScanner = new Scanner(input);
PrintWriter pw = new PrintWriter(output);
pw.println("hello, world!");
pw.print("once more");
fileScanner.close();
pw.close(); // If you open it, close it!
}
}
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class Files-TypicalProcessing {
public static void main(String [] args) throws FileNotFoundException {
File input = new File("input.txt");
File output = new File("output.txt");
Scanner fileScanner = new Scanner(input);
PrintWriter pw = new PrintWriter(output);
while(fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
System.out.println(line.charAt(0));
pw.println(line.charAt(line.length()-1));
}
fileScanner.close();
pw.close();
}
}
2. Writing code to process files
a. Write a program that copies the contents of "input.txt" to "output.txt", word for word.
b. Modify your program so that it only copies the first word of every line to "output.txt".
c. Write another variation so that "output.txt" is a copy of "input.txt", but all in lower case.
The ".toLowerCase()" method in the String class will help.
d. *Write another variation so that it converts "output.txt" into a pig-Latin version of
"input.txt". Pig-Latin is a way of modifying words so that the first letter of a word gets
moved to the end, and a suffix "ay" is added on to the end. So the first few words of
"output.txt" should look like: hisTay lanetpay ashay
You should only need the usual String operations to convert a word to pig-Latin (charAt,
substring, and +).
Download