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