IO Flashcards

1
Q

File class

A

used to read information about existing les and directories, list the contents of a directory, and create/delete les and directories.

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

Two ways to retrieve the file separator character for the current file system

A

System.getProperty(“file.separator”)

java.io.File.separator

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

What does the following code do?

File file = new File("/home/smith/data/zoo.txt");
System.out.println(file.exists());
A

Checks if the file exists within the file system.

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

How to create a new File object

A

File file = new File(String path);

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

Difference between the “Stream” named classes and the “Reader/Writer” classes

A

The stream classes are used for inputting and outputting all types of binary or byte data, whereas the reader/writer classes can only work with characters and strings

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

What does it mean when a class has the word “Buffered” in it?

A

The class reads and writes data in groups rather than a single byte or character at a time.

Unless you are doing something very specialized in your application, you should always wrap a file stream with a Buffered class in practice.

You create these instances by wrapping the corresponding stream type, which means, for example, that if you had a BufferedWriter, you would create it with: 
 new BufferedWriter(new FileWriter(filename))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Where should you open your file streams?

A

In the opening of a try-with-resources statement so that it will close at the end. Otherwise you must call .close() on it when you are done

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

flush()

A

When called on a stream, requests that all accumulated data be written immediately to disk.

Useful for if the application terminates unexpectedly

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

What should you do before calling the mark(int) method on a stream?

A

Call markSupported() on the stream to make sure it supports mark(int). Otherwise an exception will be thrown if it doesn’t support it.

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

mark(int num)

A

Sets a marker at the current point in the stream and allows you to read up to num amount of bytes with the ability to call reset() if you want to return to the original marked point

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

skip(long num)

A

Skips over the next num bytes, or if there are less than num bytes left then skips to the end.

Returns the number of bytes skipped.

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

How do you know when a FileInputStream object has reached the end of the file?

A

It returns -1

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

What exception is thrown if the file you try to read from doesn’t exist?

A

FileNotFoundException

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

copy(File file1, File file2)

A

Copies the contents of file1 to file2, overwriting any content that file2 may already have

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

write(byte[],int,int)

A

Used with BufferedOutputStream, stores the bytes in the byte array until filled with the third arguments value of bytes. The second argument is an offset to start writing which is usually set to 0

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

serialization

A

The process of converting an in-memory object to a stored data format

17
Q

Serializable interface

A

The interface that classes must implement if you are going to serialize objects of that type

Any class can implement the Serializable interface since there are no required methods to implement.

18
Q

NotSerializableException

A

Thrown when attempting to serialize an object when its class or an object contained in its class is not marked as Serializable

19
Q

Which members of a class will be ignored when serializing?

A

Those marked static or transient

20
Q

How to create an ObjectInputStream

A

ObjectInputStream in = new ObjectInputStream (new BufferedInputStream( new FileInputStream(filename)));

21
Q

Method to deserialize an object given

ObjectInputStream in = new ObjectInputStream (new BufferedInputStream( new FileInputStream(filename)));

A

Object object = in.readObject();

Note: you need to do an explicit cast if you want it to be something other than Object

22
Q

Method to serialize an object given

ObjectOutputStream out = new ObjectOutputStream(
new BufferedOutputStream(new FileOutputStream(filename)))
A

out.writeObject(objectName);

23
Q

How do we know when an ObjectInputStream has reached the end of the input file?

A

An EOFException is thrown.

24
Q

What do ObjectInputStream and ObjectOutputStream do with null values?

A

Accept them!

25
Q

What is ignored during the deserialization (writing) process?

A

The constructor and initialization blocks

26
Q

PrintStream

A

Operates on OutputStream objects and writes data as bytes

27
Q

PrintWriter

A

Operates on Writer objects and writes data as bytes

28
Q

What type of object are System.out and System.err?

A

PrintStream

29
Q

PrintWriter methods

print()

println()

format()

printf()

A

prints the argument by calling String.valueOf on it

Like print() but with a new line at the end

Takes a string and prints it based on the formatting arguments you give it

Same thing as format(), just there to make it easier on C programmers

30
Q

How to obtain the console object

A

Console console = System.console()

Note: will return null if the environment doesn’t support text interactions.

31
Q

Console methods reader() and writer()

A

Provide access to an instance of Reader and PrintWriter, respectively

32
Q

Console method format()

A

Lets you just pass it a string to print so that you don’t need to use the reader() or writer() method then print()

33
Q

Console method readLine(prompt)

A

Displays the String prompt to the user and accepts input terminated by the Enter key

34
Q

Console method readPassword()

A

functions the same as the readLine method but does not show the text being typed.

Also returns an array of chars instead of a String to avoid having it be stored in the String pool