Java OCA OCP Practice Question 520

Question

A file is created with the following code:

1. FileOutputStream fos = new FileOutputStream("datafile"); 
2. DataOutputStream dos = new DataOutputStream(fos); 
3. for (int i=0; i<500; i++) 
4.     dos.writeInt(i); 

You would like to write code to read back the data from this file.

Which solutions will work?

Choose all that apply.

  • A. Construct a FileInputStream, passing the name of the file. Onto the FileInputStream, chain a DataInputStream, and call its readInt() method.
  • B. Construct a FileReader, passing the name of the file. Call the file reader's readInt() method.
  • C. Construct a PipedInputStream, passing the name of the file. Call the piped input stream's readInt() method.
  • D. Construct a RandomAccessFile, passing the name of the file. Call the random access file's readInt() method.
  • E. Construct a FileReader, passing the name of the file. Onto the FileReader, chain a DataInputStream, and call its readInt() method.


A, D.

Note

Option A chains a data input stream onto a file input stream.

D simply uses the RandomAccessFile class.

B fails because the FileReader class has no readInt() method; readers and writers handle only text.

C fails because the PipedInputStream class has nothing to do with file I/O.

(Piped input and output streams are used in inter-thread communication.)

E fails because you cannot chain a data input stream onto a file reader.

Readers read chars, and input streams handle bytes.




PreviousNext

Related