8. I/0 Flashcards
FileInputStream constructors
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.
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.”
int available()
How to close FileInputStream?
It implements AutoCloseable interface, so one can use try with resources syntax or simply call void close() method.
FileInputStream method that “Reads a byte of data from this input stream”
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.
FileInputStream method that “Reads up to b.length bytes of data from this input stream into an array of bytes”
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.
FileInputStream method that “Skips over and discards n bytes of data from the input stream.”
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.
FileOutputStream constructors
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.
FileOutputStream method that “Writes b.length bytes from the specified byte array to this file output stream”
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.
FileOutputStream method that “Writes the specified byte to this file output stream”
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.
FileReader constructors
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.
BufferedReader constructors
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.
BufferedReader method that “Returns a Stream, the elements of which are lines read from this BufferedReader”
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.
BufferedReader method that “Reads a line of text”
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
PrintWriter method that “Appends the specified character to this writer”
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)
PrintWriter method that “Writes a formatted string to this writer using the specified format string and arguments”
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