8. I/0 Flashcards

1
Q

FileInputStream constructors

A

FileInputStream(File file)
Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system.

FileInputStream(String name)
Creates a FileInputStream by opening a connection to an actual file, the file named by the path name name in the file system.

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

FileInputStream method that “Returns an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.”

A

int available()

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

How to close FileInputStream?

A

It implements AutoCloseable interface, so one can use try with resources syntax or simply call void close() method.

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

FileInputStream method that “Reads a byte of data from this input stream”

A

public int read() throws IOException

Reads a byte of data from this input stream. This method blocks if no input is yet available.

Returns:
the next byte of data, or -1 if the end of the file is reached.

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

FileInputStream method that “Reads up to b.length bytes of data from this input stream into an array of bytes”

A

public int read(byte[] b) throws IOException

Reads up to b.length bytes of data from this input stream into an array of bytes. This method blocks until some input is available.

Parameters:
b - the buffer into which the data is read.

Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.

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

FileInputStream method that “Skips over and discards n bytes of data from the input stream.”

A

public long skip(long n) throws IOException

Skips over and discards n bytes of data from the input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. If n is negative, the method will try to skip backwards. In case the backing file does not support backward skip at its current position, an IOException is thrown. The actual number of bytes skipped is returned. If it skips forwards, it returns a positive value. If it skips backwards, it returns a negative value.

This method may skip more bytes than what are remaining in the backing file. This produces no exception and the number of bytes skipped may include some number of bytes that were beyond the EOF of the backing file. Attempting to read from the stream after skipping past the end will result in -1 indicating the end of the file.

Parameters:
n - the number of bytes to be skipped.

Returns:
the actual number of bytes skipped.

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

FileOutputStream constructors

A

FileOutputStream(File file)

Creates a file output stream to write to the file represented by the specified File object.

FileOutputStream(File file, boolean append)
Creates a file output stream to write to the file represented by the specified File object.

FileOutputStream(String name)
Creates a file output stream to write to the file with the specified name.

FileOutputStream(String name, boolean append)
Creates a file output stream to write to the file with the specified name.

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

FileOutputStream method that “Writes b.length bytes from the specified byte array to this file output stream”

A

public void write(byte[] b) throws IOException

Writes b.length bytes from the specified byte array to this file output stream.

Parameters:
b - the data.

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

FileOutputStream method that “Writes the specified byte to this file output stream”

A

public void write(byte[] b) throws IOException

Writes b.length bytes from the specified byte array to this file output stream.

Parameters:
b - the data.

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

FileReader constructors

A

FileReader(File file)
Creates a new FileReader, given the File to read from.

FileReader(String fileName)
Creates a new FileReader, given the name of the file to read from.

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

BufferedReader constructors

A

BufferedReader(Reader in)
Creates a buffering character-input stream that uses a default-sized input buffer.

BufferedReader(Reader in, int sz)
Creates a buffering character-input stream that uses an input buffer of the specified size.

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

BufferedReader method that “Returns a Stream, the elements of which are lines read from this BufferedReader”

A

public Stream<String> lines()</String>

Returns a Stream, the elements of which are lines read from this BufferedReader. The Stream is lazily populated, i.e., read only occurs during the terminal stream operation.
The reader must not be operated on during the execution of the terminal stream operation. Otherwise, the result of the terminal stream operation is undefined.

After execution of the terminal stream operation there are no guarantees that the reader will be at a specific position from which to read the next character or line.

If an IOException is thrown when accessing the underlying BufferedReader, it is wrapped in an UncheckedIOException which will be thrown from the Stream method that caused the read to take place. This method will return a Stream if invoked on a BufferedReader that is closed. Any operation on that stream that requires reading from the BufferedReader after it is closed, will cause an UncheckedIOException to be thrown.

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

BufferedReader method that “Reads a line of text”

A

public String readLine() throws IOException

Reads a line of text. A line is considered to be terminated by any one of a line feed (‘\n’), a carriage return (‘\r’), or a carriage return followed immediately by a linefeed.

Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

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

PrintWriter method that “Appends the specified character to this writer”

A

public PrintWriter append(char c)

Appends the specified character to this writer.
An invocation of this method of the form out.append(c) behaves in exactly the same way as the invocation out.write(c)

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

PrintWriter method that “Writes a formatted string to this writer using the specified format string and arguments”

A

public PrintWriter format(String format, Object… args)

Writes a formatted string to this writer using the specified format string and arguments. If automatic flushing is enabled, calls to this method will flush the output buffer.
The locale always used is the one returned by Locale.getDefault(), regardless of any previous invocations of other formatting methods on this object.

Parameters:
format - A format string as described in Format string syntax.
args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java™ Virtual Machine Specification. The behaviour on a null argument depends on the conversion.

Returns:
This writer

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

PrintWriter “A convenience method to write a formatted string to this writer using the specified format string and arguments”

A

public PrintWriter printf(String format, Object… args)

A convenience method to write a formatted string to this writer using the specified format string and arguments. If automatic flushing is enabled, calls to this method will flush the output buffer.
An invocation of this method of the form out.printf(format, args) behaves in exactly the same way as the invocation out.format(format, args)

Parameters:
format - A format string as described in Format string syntax.
args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java™ Virtual Machine Specification. The behaviour on a null argument depends on the conversion.

Returns:
This writer

17
Q

Which methods on PrintWriter can be used to print String with new line and without?

A

public void println() and public void print()

18
Q

Which stream objects implement formatting?

A

PrintWriter and PrintStream

19
Q

How to obtain Console instance?

A

Console c = System.console()

There is only one console available, it might be null if it’s not available (the program isn’t run in interactive mode)

20
Q

What are two methods to write to Console, without requiring the input?

A

Console format(String fmt, Object… args)
Writes a formatted string to this console’s output stream using the specified format string and arguments.

Console printf(String format, Object… args)
A convenience method to write a formatted string to this console’s output stream using the specified format string and arguments.

21
Q

What is the Console method that “Provides a formatted prompt, then reads a single line of text from the console”?

A

public String readLine(String fmt, Object… args)

Provides a formatted prompt, then reads a single line of text from the console.

Parameters:
fmt - A format string as described in Format string syntax.
args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java™ Virtual Machine Specification.

Returns:
A string containing the line read from the console, not including any line-termination characters, or null if an end of stream has been reached.

22
Q

What is the Console method that “Reads a single line of text from the console”?

A

public String readLine()

Reads a single line of text from the console.

Returns:
A string containing the line read from the console, not including any line-termination characters, or null if an end of stream has been reached.

23
Q

What are the Console methods for reading passwords?

A

char[] readPassword()
Reads a password or passphrase from the console with echoing disabled

char[] readPassword(String fmt, Object… args)
Provides a formatted prompt, then reads a password or passphrase from the console with echoing disabled.

24
Q

How to serialize objects using streams?

A

Use ObjectOutputStream writeObject(Object o) method.

Write the specified object to the ObjectOutputStream. The class of the object, the signature of the class, and the values of the non-transient and non-static fields of the class and all of its supertypes are written. Default serialization for a class can be overridden using the writeObject and the readObject methods. Objects referenced by this object are written transitively so that a complete equivalent graph of objects can be reconstructed by an ObjectInputStream.
Exceptions are thrown for problems with the OutputStream and for classes that should not be serialized. All exceptions are fatal to the OutputStream, which is left in an indeterminate state, and it is up to the caller to ignore or recover the stream state.

25
Q

How to deserialize objects using streams?

A

Use ObjectInputStream Object readObject() method.

Read an object from the ObjectInputStream. The class of the object, the signature of the class, and the values of the non-transient and non-static fields of the class and all of its supertypes are read. Default deserializing for a class can be overridden using the writeObject and readObject methods. Objects referenced by this object are read transitively so that a complete equivalent graph of objects is reconstructed by readObject.
The root object is completely restored when all of its fields and the objects it references are completely restored. At this point the object validation callbacks are executed in order based on their registered priorities. The callbacks are registered by objects (in the readObject special methods) as they are individually restored.

Exceptions are thrown for problems with the InputStream and for classes that should not be deserialized. All exceptions are fatal to the InputStream and leave it in an indeterminate state; it is up to the caller to ignore or recover the stream state.

If readObject() doesn’t return the object type expected, attempting to cast it to the correct type may throw a ClassNotFoundException.

26
Q

How to get the operating system-dependent file separator from the JVM?

A

System.getProperty(“path.separator”)