Chapter 19 - Binary I/O Flashcards

1
Q

What is a text file, and what is a binary file?

A

A text file is a file that can be processed (read, created, or modified) using a text editor such as Notepad on Windows.
All other files (files that are not text files) are binary files. You cannot read binary files using a text editor - they are designed to be read by programs.
Although it is not technically precise and correct, you can envision a text file as consisting of a sequence of characters and a binary file as consisting of a sequence of bits.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you write or read text data to/from a file?

A
Using a File object that encapsulates the properties of a file or a path, you use the PrintWriter class to write to a file:
PrintWriter output = new PrintWriter(file);
output.print("noodles");
And use the Scanner class to read from a file:
Scanner input = new Scanner(file);
String str = input.nextLine();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

An input object is also called ____.

An output object is also called ____

A

An input stream

An output stream

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a stream?

A

A stream is an object that connects a program to a file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the difference between text I/O and binary I/O?

A

Binary I/O is more efficient because binary I/O does not require encoding and decoding.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Describe the classes java.io.InputStream and java.io.OutputStream

A

InputStream and OutputStream are abstract classes. They are the root classes for reading and writing binary data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
The java.io.InputStream class has one abstract method (and several concrete ones). What is the abstract method?
The same is true for java.io.OutputStream. What is OutputStream's abstract method?
A

java. io.InputStream’s abstract method is ‘public int read()’. It is meant to read and return the next byte of data from the input stream. The value is returned as an int value in the range 0 to 255. If the end of the stream is reached, it returns -1.
java. io.OutputStream’s abstract method is ‘public void write(int b)’. It writes the specified byte to the output stream. The parameter b is an int value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What classes do we use for reading/writing bytes from/to files?

A

We use FileInputStream and FileOutputStream. All the methods in these classes are inherited from InputStream and OutputStream. FileInputStream/FileOutputStream does not introduce new methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you create a java.io.FileInputStream?

A

java.io.FileInputStream has two constructors:
+FileInputStream(file: File);
+FileInputStream(filename: String)

Example:
FileInputStream input =
new FileInputStream(“filename.binary”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you create a java.io.FileOutputStream?

A

java.io.FileOutputStream has four constructors:
+FileOutputStream(file: File);
+FileOutputStream(filename: String);
+FileOutputStream(file, File, append: boolean);
+FileOutputStream(filename: String, append: boolean);

The first two constructors will delete the current content of the file, while the last two constructors will keep the content and append new data to the end of the file, if the second parameter given is ‘true’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What exception may the I/O classes throw?

A

They throw java.io.IOException. Therefore, you have to declare java.io.IOException to throw in the method or place the code in a try-catch block.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is important when finished writing to a file?

A

Always close it:

output.close();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can you use instances of FileInputStream/FileOutputStream to construct a Scanner/PrintWriter?

A

Yes.
PrintWriter output =
new PrintWriter(new FileOutputStream(“temp.txt”, true));
This will create an output stream to the file temp.txt, and will keep any data present, and append to the end of the file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a filter stream?

A

Filter streams are streams that filter bytes for some purpose. The basic byte input stream provides a read method that can be used only for reading bytes. If you want to read integers, doubles, or strings, you need a filter class to wrap the byte input stream.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the base classes for filtering data streams called?

A

FilterInputStream and FilterOutputStream.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Describe DataInputStream DataOutputStream.

A

DataInputStream reads bytes from the stream and converts them into appropriate primitive type values or strings.
DataOutputStream converts primitive type values or strings into bytes and outputs the bytes to the stream.
They extend FilterInputStream / FilterOutputStream, and implements the DataInput/DataOutput interfaces.
They read and write Java primitive type values and strings in a machine-independent fashion, thereby enabling you to write a data file on one machine and read it on another machine that has a different operating system or file structure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are the methods contained in DataInputStream and DataOutputStream?

A

They contain methods for reading and writing different primitive types:
+readBoolean(): boolean
+writeBoolean(b: boolean): void
and similar methods for reading/writing byte, char, float, double, int long, short,

18
Q

How do you construct a DataInputStream / DataOutputStream?

A
The constructors:
public DataInputStream(InputStream instream)
public DataOutputStream(OutputStream outstream)
Example:
DataInputStream input = 
new DataInputStream(new
FileInputStream("in.binary"));
DataOutputStream output = 
new DataOutputStream(new
FileOutputStream("out.binary"));
19
Q

If you keep reading data at the end of an InputStream, what will happen?

A

An EOFException will occur. This exception can be used to detect the end of a file.

20
Q

How is BufferedInputStream / BufferedOutputStream different from DataInputStream / DataOutputStream?

A

They are exactly the same – they contain the same methods – except that BufferedInputStream / BufferedOutputStream manages a buffer behind the scene. They speed up input and output by reducing the number of disk reads and writes.
Using BufferedInputStream, the whole block of data on the disk is read into the buffer in the memory once. The individual data are then delivered to your program from the buffer. Using BufferedOutputStream, the individual data are first written to the buffer in the memory. When the buffer is full, all data in the buffer is written to the disk once.

21
Q

What is the default size of the buffer (BufferedInputStream)?

A

The default size is 512 bytes.

22
Q

When should you use buffered I/O?

A

You should (nearly) always use buffered I/O to speed up input and output. For small files, you may not notice performance improvements. However, for large files – over 100 MB – you will see substantial improvements using buffered I/O.

23
Q

What byte value signifies the end of a file?

A

-1

24
Q

Describe ObjectInputStream / ObjectOutputStream.

A

ObjectInputStream / ObjectOutputStream contains all the functions of DataInputStream / DataOutputStream, but also enables you to perform I/O for objects.
When using ObjectInputStream, remember that it may throw a ClassNotFoundException. Also, when reading objects from an input stream, you must type-cast it back to the original type.

25
Q

What is the Serializable interface?

A
Not every object can be written to an output stream. Objects that can be written so are said to be serializable. A serializable object must implement the Serializable interface. The Serializable interface is a marker interface, which means that it has no methods, and you don't need to add any additional code to your class that implements the interface.
Implementing this interface enables the Java serialization mechanism to automate the process of storing objects and arrays.
26
Q

What is meant by “object serialization” and “object deserialization”?

A

Writing objects to a file is referred to as object serialization. Reading objects is called object deserialization.

27
Q

If an object is an instance of Serializable but contains nonserializable instance data fields, can it be serialized?

A

No. To enable the object to be serialized, mark these data fields with the ‘transient’ keyword to tell the JVM to ignore them when writing the object to an object stream.

28
Q

If an object is written to an object stream more than once, will it be stored in multiple copies?

A

No, it will not. When an object is written for the first time, a serial number is created for it. The JVM writes the complete contents of the object along with the serial number into the object stream. After the first time, only the serial number is stored if the same object is written again. When the objects are read back, their references are the same, since only once object is actually created in memory.

29
Q

Can an array be serialized?

A

Yes, if all its elements are serializable. An entire array can be saved into a file using writeObject and later can be restored using readObject.

30
Q

Describe the java.io.RandomAccessFile class.

A

Using RandomAccessFile, you can create an in-out stream – a data stream that allows you to both read and write from/to a file. RandomAccessFile implements DataInput and DataOutput, which gives it the same methods as DataInputStream and DataOutputStream. The constructor takes two parameters: the first is the name of file, the second is the mode; mode “r” gives an input stream, while mode “rw” gives an in-out stream. The class contains methods for working with the “file pointer” (the location of the next read/write), which enables you to write/read to/from a location of choice.

31
Q

Which of the following statements are true?

A. A File object encapsulates the properties of a file or a path, but does not contain the methods for reading/writing.
B. You can use the PrintWriter class for outputting text to a file.
C. You can use the Scanner class for reading text from a file.
D. An input object is also called an input stream.
E. An output object is also called an output stream.

A

All are true.

32
Q

Which of the following statements are true?

A. Text I/O is built upon binary I/O to provide a level of abstraction for character encoding and decoding.
B. Text I/O involves encoding and decoding.
C. Binary I/O does not require conversions.
D. Binary I/O is more efficient than text I/O, because binary I/O does not require encoding and decoding.
E. Binary files are independent of the encoding scheme on the host machine and thus are portable.

A

All are true.

33
Q

Which method can you use to find out the number of the bytes in a file using InputStream?

A. length()
B. available()
C. size()
D. getSize()

A

B is correct.

34
Q

Which of the following statements are true?
A. All methods in FileInputStream/FileOutputStream are inherited from InputStream/OutputStream.
B. You can create a FileInputStream/FileOutputStream from a File object or a file name using FileInputStream/FileOutputStream constructors.
C. The return value -1 from the read() method signifies the end of file.
D. A java.io.FileNotFoundException would occur if you attempt to create a FileInputStream with a nonexistent file.
E. A java.io.FileNotFoundException would occur if you attempt to create a FileOutputStream with a nonexistent file.

A

All but E are true.

35
Q

To append data to an existing file, use _____ to construct a FileOutputStream for file out.dat.

A. new FileOutputStream("out.dat")
B. new FileOutputStream("out.dat", false)
C. new FileOutputStream("out.dat", true)
D. new FileOutputStream(true, "out.dat")
A

The correct answer is C.

36
Q
What does the following code do?
FileInputStream fis = new FileInputStream("test.dat");

A. It creates a new file named test.dat if it does not exist and opens the file so you can write to it.
B. It creates a new file named test.dat if it does not exist and opens the file so you can write to it and read from it.
C. It creates a new file named test.dat regardless of whether it exists or not and opens the file so you can write to it.
D. It creates a new file named test.dat regardless of whether it exists or not and opens the file so you can write to it and read from it.
E. It creates a FileInputStream for test.dat if test.dat exists.

A

The correct answer is E.

37
Q

Which of the following statements is correct to create a DataOutputStream to write to a file named out.dat?

A. DataOutputStream outfile = new DataOutputStream(new File("out.dat"));
B. DataOutputStream outfile = new DataOutputStream(new FileOutputStream("out.dat"));
C. DataOutputStream outfile = new DataOutputStream(FileOutputStream("out.dat"));
D. DataOutputStream outfile = new DataOutputStream("out.dat");
A

The correct answer is B.

38
Q

Which of the following statements are true?

A. ObjectInputStream/ObjectOutputStream enables you to perform I/O for objects in addition for primitive type values and strings.
B. Since ObjectInputStream/ObjectOutputStream contains all the functions of DataInputStream/DataOutputStream, you can replace DataInputStream/DataOutputStream completely by ObjectInputStream/ObjectOutputStream.
C. To write an object, the object must be serializable.
D. The Serializable interface does not contain any methods. So it is a mark interface.
E. If all the elements in an array is serializable, the array is serializable too.

A

All are true.

39
Q

The Loan class given in the text does not implement java.io.Serializable. Analyze the following code.

public class Foo implements java.io.Serializable {  
  private int v1;
  private static double v2;
  private Loan v3 = new Loan();
}

A. An instance of Foo can be serialized because Foo implements Serializable.
B. An instance of Foo cannot be serialized because Foo contains a non-serializable instance variable v3.
C. If you mark v3 as transient, an instance of Foo is serializable.

A
The correct answer is BC
Explanation: An object may not be serialized even though its class implements java.io.Serializable, because it may contain non-serializable instance variables.
40
Q

With which I/O class can you append or update a file?

A. RandomAccessFile(),
B. OutputStream()
C. DataOutputStream()
D. None of the above

A

The correct answer is A.