Ch19 I/O Flashcards

1
Q

What package can be used to interact with files and streams (I/O) in Java?

A

java.io

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

What is the File class used for?

A

The File class is used to read information about existing files and directories, list the contents of a directory, and create/delete files and directories.

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

What are two ways to retrieve the local separator character?

A
  • System.getProperty(“file.separator”).
  • java.io.File.separator.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What constructors (and parameters) does the File class have?

A
  • public File(String pathname)
  • public File(File parent, String child)
  • public File(String parent, String child)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Most InputStream stream classes have a corresponding OutputStream class and most Reader classes have a corresponding Writer class.

example: FileInputStream and FileOutputStream, FileWriter and FileReader.

What classes are an exception to this rule?

A
  • PrintWriter has no accompanying PrintReader class.
  • PrintStream (is an OutputStream) has no accompanying form of InputStream
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between a low-level and high-level stream?

A

A low-level stream connects directly with the source of the data while a high-level stream is built on top of another stream using wrapping.

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

What are the four abstract classes that are the parents of all stream classes in the java.io library?

A
  • InputStream
  • OutputStream
  • Reader
  • Writer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why does the following code not compile?

new BufferedInputStream(new InputStream());

A

InputStream is abstract and cannot be instantiated

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

What is the ObjectInputStream class used for? is it high/low level?

A

Deserializes primitive Java data types and graphs of Java objects from an existing InputStream

High level

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

What is the PrintStream class used for? is it high/low level?

A

Writes formatted representations of Java objects to a binary stream

High level

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

When working with a wrapper stream, do we have to close the wrapped stream before closing the wrapper stream when we want to close resources?

A

No.

It is only necessary to use close() on the topmost object.

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

What methods exist to manipulate the order in which data is read from a stream?

A
  • markSupported()
  • mark(int readLimit)
  • reset() throws IOException
  • skip(long n) throws IOException
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the rules for a Serializable class?

A
  • The class must implement Serializable
  • Every instance member of the class is serializable, marked transient, or has a null value at the time of serialization
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Is the following Cat class serializable?

public class Cat implements Serializable {
private Tail tail = new Tail();
}
public class Tail implements Serializable {
private Fur fur = new Fur();
}
public class Fur { }

A

Cat and Tail both are marked Serializable, but Fur is not, making the Cat class not serializable.

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

Is the following Cat class serializable?

public class Cat implements Serializable {
private Tail tail = new Tail();
}
public class Tail implements Serializable {
private transient Fur fur = new Fur();
}
public class Fur { }

A

Cat and Tail both are marked Serializable, and Fur is transient, making the Cat class serializable.

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

In which of the following cases can the Console object be acquired?

A. When the JVM is started from an interactive command line with explicitly redirecting the standard input and output streams to Console.

B. When the JVM is started from an interactive command line without redirecting the standard input and output streams.

C. When the JVM is started in the background with the standard input and output streams directed to Console.

D. When the JVM is started in the background without redirecting the standard input and output streams.

A

B.

Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.

If this virtual machine has a console then it is represented by a unique instance of this class which can be obtained by invoking the System.console() method. If no console device is available then an invocation of that method will return null.

17
Q

What are the rules of serialPeristentFields?

A

The modifiers for the field are required to be private, static, and final. If the field’s value is null or is otherwise not an instance of ObjectStreamField[], or if the field does not have the required modifiers, then the behavior is as if the field were not declared at all.

For example, the following declaration duplicates the default behavior.
class List implements Serializable {
List next;
private static final ObjectStreamField[] serialPersistentFields
= {new ObjectStreamField(“next”, List.class)};

}

By using serialPersistentFields to define the Serializable fields for a class, there no longer is a limitation that a serializable field must be a field within the current definition of the Serializable class.

18
Q

What are the approaches for handling sensitive fields in serializable classes?

A
  1. Declare sensitive fields transient
  2. Define the serialPersistentFields array field appropriately
  3. Implement writeObject and use ObjectOutputStream.putField selectively
  4. Implement writeReplace to replace the instance with a serial proxy
  5. Implement the Externalizable interface