File Handle
An open file for reading and writing; using either streaming and random access.
Use read and write to perform one-off random-access reads and writes. Use source, sink, and appendingSink for streaming reads and writes.
File handles must be closed when they are no longer needed. It is an error to read, write, or create streams after a file handle is closed. The operating system resources held by a file handle will be released once the file handle and all of its streams are closed.
Although this class offers both reading and writing APIs, file handle instances may be read-only or write-only. For example, a handle to a file on a read-only file system will throw an exception if a write is attempted.
File handles may be used by multiple threads concurrently. But the individual sources and sinks produced by a file handle are not safe for concurrent use.
Properties
Functions
Returns a sink that writes to this starting at the end. The returned sink must be closed when it is no longer needed.
Returns the position of sink in the file. The argument sink must be either a sink produced by this file handle, or a BufferedSink that directly wraps such a sink. If the parameter is a BufferedSink, it adjusts for buffered bytes.
Returns the position of source in the file. The argument source must be either a source produced by this file handle, or a BufferedSource that directly wraps such a source. If the parameter is a BufferedSource, it adjusts for buffered bytes.
Reads at least 1, and up to byteCount bytes from this starting at fileOffset and appends them to sink. Returns the number of bytes read, or -1 if fileOffset equals size.
Reads at least 1, and up to byteCount bytes from this starting at fileOffset and copies them to array at arrayOffset. Returns the number of bytes read, or -1 if fileOffset equals size.
Change the position of sink in the file to position. The argument sink must be either a sink produced by this file handle, or a BufferedSink that directly wraps such a sink. If the parameter is a BufferedSink, it emits for buffered bytes.
Change the position of source in the file to position. The argument source must be either a source produced by this file handle, or a BufferedSource that directly wraps such a source. If the parameter is a BufferedSource, it will skip or clear buffered bytes.
Returns a sink that writes to this starting at fileOffset. The returned sink must be closed when it is no longer needed.
Returns a source that reads from this starting at fileOffset. The returned source must be closed when it is no longer needed.