Page 670 - Introduction to Programming with Java: A Problem Solving Approach
P. 670
636
Chapter 15 Files
8.
Assume you have successfully opened this text file for input and given the connection the name fileIn. Assume the next lines of code are:
double num = fileIn.nextDouble();
String name = fileIn.nextLine();
What is the length of the final string in name? Explain. Given this array:
int[] number = new int[] {2, 3, 4};
Suppose you use the following code to write the three elements of the number array into a text file: for (i=0; i<3; i++)
{
}
fileOut.print(Integer.toString(number[i]));
Then, if you used the ReadTextFile program modified to read int values, what value would number[0] have?
§15.5 HTML File Generator
9. Assuming the object that manages output will be called writer, write a statement that opens a text file
called dogs.html for output by println statements.
10. Where do the <h1> and </h1> tags go in an HTML file?
11. Write a statement that breaks a file-output connection called writer.
§15.7 Binary File I/O
14. Write a statement that opens a new binary file for output and assigns a reference to the connection to
binaryOut. Put the file in the current directory, with the name windSpeed.data.
§15.8 Object File I/O
15. Write a statement that opens a file for input of objects and assigns a reference to the connection to
objectIn. Assume the file is in the current directory, with the name automobiles.data. §15.9 The File Class
16. Write a code fragment that lists all the files in the directory that contains the currently executing program.
Exercises
1. [after §15.2] What is the principal advantage of each of the three types of file I/O—text, binary, and object?
2. [after §15.3] If you forget to close an input file, it may cause your computer’s system performance to degrade. (T / F)
Apago PDF Enhancer
§15.6 Text File Data Format Versus Binary File Data Format
12. In a text file, each character consumes only one byte (8 bits) of memory. (T / F)
13. In a binary file, each character normally consumes two bytes (16 bits) of memory. (T / F)