Ch19 I/O Flashcards
What package can be used to interact with files and streams (I/O) in Java?
java.io
What is the File class used for?
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.
What are two ways to retrieve the local separator character?
- System.getProperty(“file.separator”).
- java.io.File.separator.
What constructors (and parameters) does the File class have?
- public File(String pathname)
- public File(File parent, String child)
- public File(String parent, String child)
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?
- PrintWriter has no accompanying PrintReader class.
- PrintStream (is an OutputStream) has no accompanying form of InputStream
What is the difference between a low-level and high-level stream?
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.
What are the four abstract classes that are the parents of all stream classes in the java.io library?
- InputStream
- OutputStream
- Reader
- Writer
Why does the following code not compile?
new BufferedInputStream(new InputStream());
InputStream is abstract and cannot be instantiated
What is the ObjectInputStream class used for? is it high/low level?
Deserializes primitive Java data types and graphs of Java objects from an existing InputStream
High level
What is the PrintStream class used for? is it high/low level?
Writes formatted representations of Java objects to a binary stream
High level
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?
No.
It is only necessary to use close() on the topmost object.
What methods exist to manipulate the order in which data is read from a stream?
- markSupported()
- mark(int readLimit)
- reset() throws IOException
- skip(long n) throws IOException
What are the rules for a Serializable class?
- 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
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 { }
Cat and Tail both are marked Serializable, but Fur is not, making the Cat class not serializable.
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 { }
Cat and Tail both are marked Serializable, and Fur is transient, making the Cat class serializable.