Chapter 19: 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.
Given the following code, how can we check to see if the file exists?
File zooFile = new File(“data/stripes.txt”);
zooFile.exists();
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)
True or false? The following File instance points to a file.
var someFile = new File(“/data/zoo.txt”);
Maybe.
/data/zoo.txt looks like a file but could be a directory. Don’t assume it is either unless the question tells you it is.
The java.io API defines two sets of stream classes for reading and writing streams. What are they and what are their differences?
- Byte streams
- Character streams
Byte streams read/write binary data and have class names that end with InputStream or OutputStream.
Character streams read/write text data and have class names that end in Reader or Writer.
Why aren’t character streams strictly necessary to be used for io?
Since the byte stream classes can write all types of binary data, including strings.
When working with text, which type of stream class should be used?
Character streams
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.
Why should you -usually- always wrap a file stream with a Buffered class?
Buffered classes read/write data in groups, rather than a single byte or character at a time. This drastically increases performance as I/O operations are very costly.
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 FileReader(“x.txt”));
BufferedInputStream takes an InputStream, not a Reader.
Why does the following code not compile?
new BufferedInputStream(new InputStream());
InputStream is abstract and cannot be instantiated
What is the abstract stream base class for all input character streams?
- Reader
What is the abstract stream base class for all output character streams?
Writer
What is the abstract stream base class for all input byte streams?
InputStream
What is the abstract stream base class for all output byte streams?
OutputStream
What is the FileInputStream class used for? is it high/low level?
Reads file data as bytes
Low level
What is the FileOutputStream class used for? is it high/low level?
Writes file data as bytes
Low level
What is the FileReader class used for? is it high/low level?
Reads file data as characters
Low level
What is the FileWriter class used for? is it high/low level?
Writes file data as characters
Low level
What is the BufferedInputStream class used for? is it high/low level?
Reads byte data from an existing InputStream in a buffered manner, which improves efficiency and performance
High level
What is the BufferedOutputStream class used for? is it high/low level?
Writes byte data to an existing OutputStream in a buffered manner, which improves efficiency and performance
High level
What is the BufferedReader class used for? is it high/low level?
Reads character data from an existing Reader in a buffered manner, which improves efficiency and performance
High level
What is the BufferedWriter class used for? is it high/low level?
Writes character data to an existing Writer in a buffered manner, which improves efficiency and performance
High level
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 ObjectOutputStream class used for? is it high/low level?
Serializes primitive Java data types and graphs of Java objects to an existing OutputStream
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
What is the PrintWriter class used for? is it high/low level?
Writes formatted representations of Java objects to a character stream
High level
All I/O streams include a method to release any resources within the stream when it is no longer needed. What method is this?
close()
True or False
All I/O streams inplement the Closeable interface?
True
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 do the mark(int readLimit) and reset() methods do? (input stream class methods)
the mark(int readLimit) and reset() methods return a stream to an earlier position.
What method should be called before calling either the mark(int readLimit) or reset methods? (input stream class methods)
markSupported(), which returns true only if mark() is supported (not all input stream classes support mark() and reset()).
What does the skip(long n) method do? (input stream class method)
The skip() method reads data from a stream and discards the contents.
Given the following code, how can we manipulate the order in which data is read such that it prints LIOION instead of LION?
... InputStream is... ... System.out.print((char) is.read()); //L System.out.print((char) is.read()); //I System.out.print((char) is.read()); //O System.out.print((char) is.read()); //N
... InputStream is... ... System.out.print((char) is.read()); //L if(is.markSupported()) { is.mark(100); // call reset after at most 100 bytes System.out.print((char) is.read()); //I System.out.print((char) is.read()); //O is.reset(); } System.out.print((char) is.read()); //I System.out.print((char) is.read()); //O System.out.print((char) is.read()); //N
Given the following code, how can we manipulate the order in which data is read such that it prints TRS instead of TIGERS?
... InputStream is... ... System.out.print((char) is.read()); //T System.out.print((char) is.read()); //I System.out.print((char) is.read()); //G System.out.print((char) is.read()); //E System.out.print((char) is.read()); //R System.out.print((char) is.read()); //S
... InputStream is... ... System.out.print((char) is.read()); //T is.skip(3); System.out.print((char) is.read()); //R System.out.print((char) is.read()); //S
What does the flush() method do in Output Streams (OutputStream and Writer)?
The flush() method requests that all accumulated data in memory be written immediately to disk, so that no data is lost when the program terminates unexpectedly.
Why is it not needed to call the flush() method when you have finished writing data?
The close() method automatically calles the flush() method too.
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.
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 implements Serializable { }
All classes are marked Serializable making the Car class serializable.
Is the following Cat class serializable?
public class Cat implements Serializable { private Tail tail = new Tail(); }
public class Tail implements Serializable { private Fur fur = null; }
public class Fur { }
Cat and Tail both are marked Serializable, and Fur is not. However, fur is assigned null. If it is null during serialization, the Cat class is serializable.