Chapter TWENTY-THREE
Java I/O Fundamentals


Exam Objectives

Read and write data from the console.
Use BufferedReader, BufferedWriter, File, FileReader, FileWriter, FileInputStream, FileOutputStream, ObjectOutputStream, ObjectInputStream, and PrintWriter in the java.io package.

Answers

1. The correct answer is D.
Option A is not valid. Writer is an abstract class and cannot be instantiated.
Option B is not valid. PrintWriter has no default constructor.
Option C is not valid. PrintWriter doesn't accept a Reader as an argument in any of its constructors.
Option D is valid. PrintWriter accepts a subclass of OutputStream as a constructor argument.


2. The correct answer is A.
The default behavior of FileOutputStream and FileWriter is to overwrite the file.


3. The correct answer is B.
System.in is an InputStream.


4. The correct answer is C.
The class the program is trying to serialize doesn't implement the java.io.Serializable interface. When that happens, the writeObject() method throws a java.io.NotSerializableException, which is an IOException. This way, the exception is caught and "Error" is printed.


5. The correct answer is D.
The flush() method writes to the disk the data cached in memory.