Page 677 - Introduction to Programming with Java: A Problem Solving Approach
P. 677

                2001
2002
13. [after §15.9] Look at the text example in Figure 15.13 and Sun’s Java documentation for the File class, and explain what the following constructor and methods do:
a) File(".")
b) getAbsoluteFile()
c) getParentFile() d) list()
Review Question Solutions
1. False. You cannot view the contents of binary or object files with text editors.
2. import java.io.*;
3. (1) Open the file. (2) Make the transfer and transform the data format. (3) Close the file.
4. Use the println method to put the distinct string on a separate line, like this:
fileOut.println(name + " " + id);
5. fileOut = new PrintWriter(
new FileOutputStream("mydata.txt", true));
6. The String-parameter Scanner constructor operates on the input string itself, not on the file it
identifies.
7. The final value of name.length() = 3. The nextDouble method reads everything up through the
last numerical digit. The nextLine method reads the following space plus the two characters in “hi.”
8. number[0] equals 234 Writing integers without a following space combines the separately output numbers into what looks like one number in the file, and a subsequent read interprets the combination as one number.
9. writer = new PrintWriter("dogs.html");
10. The <h1> and </h1> tags enclose the visible Web page header.
11. writer.close;
12. True. In a text file, each character consumes only one byte of memory.
13. True. In a binary file, each character normally consumes two bytes (16 bits) of memory.
14. DataOutputStream binaryOut = new DataOutputStream( new FileOutputStream("windSpeed.data"));
15. ObjectInputStream objectIn = new ObjectInputStream( new FileInputStream("automobiles.data"));
16. This lists all the files in the currently executing program’s current directory: String[] listing =
(new File(".")).getAbsoluteFile().getParentFile().list();
for (int i=0; i<listing.length; i++)
{
}
Apago PDF Enhancer
System.out.println(listing[i]);
Review Question Solutions 643
 
































































   675   676   677   678   679