Due to the print book page limit, we cannot inlcude all good CheckPoint questions in the physical book. The CheckPoint on this Website may contain extra questions not printed in the book. The questions in some sections may have been reordered as a result. Nevertheless, it is easy to find the CheckPoint questions in the book on this Website. Please send suggestions and errata to Dr. Liang at y.daniel.liang@gmail.com. Indicate the book, edition, and question number in your email. Thanks!

Chapter 17 Check Point Questions

Section 17.2
17.2.1
What is a text file and what is a binary file? Can you view a text file or a binary file using a text editor?
17.2.2
How do you read or write text data in Java? What is a stream?
Section 17.3
17.3.1
What are the differences between text I/O and binary I/O?
17.3.2
How is a Java character represented in the memory, and how is a character represented in a text file?
17.3.3
If you write the string "ABC" to an ASCII text file, what values are stored in the file?
17.3.4
If you write the string "100" to an ASCII text file, what values are stored in the file? If you write a numeric byte-type value 100 using binary I/O, what values are stored in the file?
17.3.5
What is the encoding scheme for representing a character in a Java program? By default, what is the encoding scheme for a text file on Windows?
Section 17.4
17.4.1
Why do you have to declare to throw IOException in the method or use a try-catch block to handle IOException for Java I/O programs?
17.4.2
Why should you always close streams? How do you close streams?
17.4.3
The read() method in InputStream reads a byte. Why does it return an int instead of a byte? Find the abstract methods in InputStream and OutputStream.
17.4.4
Does FileInputStream/FileOutputStream introduce any new methods beyond the methods inherited from InputStream/OutputStream? How do you create a FileInputStream/FileOutputStream?
17.4.5
What will happen if you attempt to create an input stream on a nonexistent file? What will happen if you attempt to create an output stream on an existing file? Can you append data to an existing file?
17.4.6
How do you append data to an existing text file using java.io.PrintWriter?
17.4.7
Suppose a file contains an unspecified number of double values that were written to the file using the writeDouble method using a DataOutputStream, how do you write a program to read all these values? How do you detect the end of a file?
17.4.8
What is written to a file using writeByte(91) on a FileOutputStream?
17.4.9
How do you check the end of a file in an input stream (FileInputStream, DataInputStream)?
17.4.10
What is wrong in the following code?
import java.io.*;

public class Test {
  public static void main(String[] args) {
    try (
      FileInputStream fis = new FileInputStream("test.dat"); ) {
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }
    catch (FileNotFoundException ex) {
      ex.printStackTrace();
    }
  }
}
17.4.11
Suppose you run the following program on Windows using the default ASCII encoding after the program is finished, how many bytes are there in the file t.txt? Show the contents of each byte.
public class Test {
  public static void main(String[] args) 
      throws java.io.IOException {
    try (java.io.PrintWriter output = 
        new java.io.PrintWriter("t.txt"); ) {
      output.printf("%s", "1234");
      output.printf("%s", "5678");
      output.close();
    }
  }
}
17.4.12
After the following program is finished, how many bytes are there in the file t.dat? Show the contents of each byte.
import java.io.*;

public class Test {
  public static void main(String[] args) throws IOException {
    try (DataOutputStream output = new DataOutputStream(
        new FileOutputStream("t.dat")); ) {
      output.writeInt(1234);
      output.writeInt(5678);
      output.close();
    }
  }
}
17.4.13
For each of the following statements on a DataOutputStream output, how many bytes are sent to the output?
output.writeChar('A');
output.writeChars("BC");
output.writeUTF("DEF");
17.4.14
What are the advantages of using buffered streams? Are the following statements correct?
BufferedInputStream input1 = 
  new BufferedInputStream(new FileInputStream("t.dat"));

DataInputStream input2 = new DataInputStream(
  new BufferedInputStream(new FileInputStream("t.dat")));

DataOutputStream output = new DataOutputStream(
  new BufferedOutputStream(new FileOutnputStream("t.dat")));
Section 17.5
17.5.1
How does the program check if a file already exists?
17.5.2
How does the program detect the end of the file while reading data?
17.5.3
How does the program count the number of bytes read from the file?
Section 17.6
17.6.1
What types of objects can be stored using the ObjectOutputStream? What is the method for writing an object? What is the method for reading an object? What is the return type of the method that reads an object from ObjectInputStream?
17.6.2
If you serialize two objects of the same type, will they take the same amount of space? If not, give an example.
17.6.3
Is it true that any instance of java.io.Serializable can be successfully serialized? Are the static variables in an object serialized? How do you mark an instance variable not to be serialized?
17.6.4
Can you write an array to an ObjectOutputStream?
17.6.5
Is it true that DataInputStream/DataOutputStream can always be replaced by ObjectInputStream/ObjectOutputStream?
17.6.6
What will happen when you attempt to run the following code?
import java.io.*;
public class Test {
  public static void main(String[] args) throws IOException {
    try ( ObjectOutputStream output = 
        new ObjectOutputStream(new FileOutputStream("object.dat")); ) {
      output.writeObject(new A());
    }
  }
}

class A implements Serializable {
  B b = new B();
}

class B { 
}
Section 17.7
17.7.1
Can RandomAccessFile streams read and write a data file created by DataOutputStream? Can RandomAccessFile streams read and write objects?
17.7.2
Create a RandomAccessFile stream for the file address.dat to allow the updating of student information in the file. Create a DataOutputStream for the file address.dat. Explain the differences between these two statements.
17.7.3
What happens if the file test.dat does not exist when you attempt to compile and run the following code?
import java.io.*;
public class Test { 
  public static void main(String[] args) {
    try ( RandomAccessFile raf =
        new RandomAccessFile("test.dat", "r"); ) {
      int i = raf.readInt();
    }
    catch (IOException ex) {
      System.out.println("IO exception");
    }
  }
}