Chapter 19: 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

Given the following code, how can we check to see if the file exists?

File zooFile = new File(“data/stripes.txt”);

A

zooFile.exists();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
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
6
Q

True or false? The following File instance points to a file.

var someFile = new File(“/data/zoo.txt”);

A

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.

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

The java.io API defines two sets of stream classes for reading and writing streams. What are they and what are their differences?

A
  • 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.

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

Why aren’t character streams strictly necessary to be used for io?

A

Since the byte stream classes can write all types of binary data, including strings.

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

When working with text, which type of stream class should be used?

A

Character streams

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
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
11
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
12
Q

Why should you -usually- always wrap a file stream with a Buffered class?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
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
14
Q

Why does the following code not compile?

new BufferedInputStream(new FileReader(“x.txt”));

A

BufferedInputStream takes an InputStream, not a Reader.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
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
16
Q

What is the abstract stream base class for all input character streams?

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

What is the abstract stream base class for all output character streams?

A

Writer

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

What is the abstract stream base class for all input byte streams?

A

InputStream

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

What is the abstract stream base class for all output byte streams?

A

OutputStream

20
Q

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

A

Reads file data as bytes

Low level

21
Q

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

A

Writes file data as bytes

Low level

22
Q

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

A

Reads file data as characters

Low level

23
Q

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

A

Writes file data as characters

Low level

24
Q

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

A

Reads byte data from an existing InputStream in a buffered manner, which improves efficiency and performance

High level

25
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
26
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
27
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
28
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
29
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
30
What is the PrintStream class used for? is it high/low level?
Writes formatted representations of Java objects to a binary stream High level
31
What is the PrintWriter class used for? is it high/low level?
Writes formatted representations of Java objects to a character stream High level
32
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()
33
True or False All I/O streams inplement the Closeable interface?
True
34
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.
35
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
36
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.
37
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()).
38
What does the skip(long n) method do? (input stream class method)
The skip() method reads data from a stream and discards the contents.
39
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 ```
40
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 ```
41
What does the flush() method do in Output Streams (OutputStream and Writer)?
The flush() method requests that all accumulated data be written immediately to disk, so that no data is lost when the program terminates unexpectedly.
42
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.
43
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
44
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.
45
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.
46
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.
47
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.