Package tkrzw
Class File
Object
File
Generic file implementation.
- Note:
- All operations except for "open" and "close" are thread-safe; Multiple threads can access the same file concurrently. You can specify a concrete class when you call the "open" method. Every opened file must be closed explicitly by the "close" method to avoid data corruption. Moreover, every unused file object should be destructed by the "destruct" method to free resources.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionlong
append
(byte[] buf) Appends data at the end of the file.long
Appends data at the end of the file.long
Appends data at the end of the file.close()
Closes the file.void
destruct()
Destructs the object and releases resources.getPath()
Gets the path of the file.long
getSize()
Gets the size of the file.Opens a file, without optional parameters.Opens a file, with a string expression for optional parameters.Opens a file.read
(long off, byte[] buf, long size) Reads data.byte[]
read
(long off, long size) Reads data and returns a byte array.byte[]
Reads data.readString
(long off, long size) Reads data and returns a byte array.readString
(long off, long size, Status status) Reads data and returns a byte array.byte[][]
Searches the file and get lines which match a pattern.String[]
Searches the file and get lines which match a pattern, with string data.synchronize
(boolean hard) Synchronizes the entire content of the file to the file system.synchronize
(boolean hard, long off, long size) Synchronizes the content of the file to the file system.toString()
Gets a string representation of the iterator.truncate
(long size) Truncates the file.write
(long off, byte[] buf) Writes data.write
(long off, byte[] buf, long size) Writes data.Writes a string.
-
Constructor Details
-
File
public File()Constructor.
-
-
Method Details
-
destruct
public void destruct()Destructs the object and releases resources.- Note:
- The file is closed implicitly if it has not been closed.
-
open
Opens a file.- Parameters:
path
- The path of the file.writable
- If true, the file is writable. If false, it is read-only.params
- Optional parameters. If it is null, it is ignored.- Returns:
- The result status.
- Note:
- The optional parameters can include options for the file opening operation.
- truncate (bool): True to truncate the file.
- no_create (bool): True to omit file creation.
- no_wait (bool): True to fail if the file is locked by another process.
- no_lock (bool): True to omit file locking.
- sync_hard (bool): True to do physical synchronization when closing.
The optional parameter "file" specifies the internal file implementation class. The default file class is "MemoryMapAtomicFile". The other supported classes are "StdFile", "MemoryMapAtomicFile", "PositionalParallelFile", and "PositionalAtomicFile".
For the file "PositionalParallelFile" and "PositionalAtomicFile", these optional parameters are supported.
- block_size (int): The block size to which all blocks should be aligned.
- access_options (str): Values separated by colon. "direct" for direct I/O. "sync" for synchrnizing I/O, "padding" for file size alignment by padding, "pagecache" for the mini page cache in the process.
-
open
Opens a file, with a string expression for optional parameters.- Parameters:
path
- A path of the file.writable
- If true, the file is writable. If false, it is read-only.params
- The optional parameter expression in "key=value,key=value" format.- Returns:
- The result status.
-
open
Opens a file, without optional parameters.- Parameters:
path
- A path of the file.writable
- If true, the file is writable. If false, it is read-only.- Returns:
- The result status.
-
close
Closes the file.- Returns:
- The result status.
-
read
Reads data.- Parameters:
off
- The offset of a source region.buf
- The destination buffer.size
- The size to be read.- Returns:
- The result status.
-
read
Reads data.- Parameters:
off
- The offset of a source region.size
- The size to be read.status
- The status object to store the result status. If it is null, it is ignored.- Returns:
- A byte array containing read data.
-
read
public byte[] read(long off, long size) Reads data and returns a byte array.- Parameters:
off
- The offset of a source region.size
- The size to be read.- Returns:
- A byte array containing read data.
-
readString
Reads data and returns a byte array.- Parameters:
off
- The offset of a source region.size
- The size to be read.status
- The status object to store the result status. If it is null, it is ignored.- Returns:
- A string containing read data.
-
readString
Reads data and returns a byte array.- Parameters:
off
- The offset of a source region.size
- The size to be read.- Returns:
- A string containing read data.
-
write
Writes data.- Parameters:
off
- The offset of the destination region.buf
- The source buffer.size
- The size to be written.- Returns:
- The result status.
-
write
Writes data.- Parameters:
off
- The offset of the destination region.buf
- The source buffer. The written size is the size of the buffer.- Returns:
- The result status.
-
write
Writes a string.- Parameters:
off
- The offset of the destination region.str
- The source string.- Returns:
- The result status.
-
append
Appends data at the end of the file.- Parameters:
buf
- The source buffer.size
- The size to be written.status
- The status object to store the result status. If it is null, it is ignored.- Returns:
- The offset at which the data has been put, or -1 on failure.
-
append
public long append(byte[] buf) Appends data at the end of the file.- Parameters:
buf
- The source buffer. The written size is the size of the buffer.- Returns:
- The offset at which the data has been put, or -1 on failure.
-
append
Appends data at the end of the file.- Parameters:
str
- The source string.- Returns:
- The offset at which the data has been put, or -1 on failure.
-
truncate
Truncates the file.- Parameters:
size
- The new size of the file.- Returns:
- The result status.
- Note:
- If the file is shrunk, data after the new file end is discarded. If the file is expanded, null codes are filled after the old file end.
-
synchronize
Synchronizes the content of the file to the file system.- Parameters:
hard
- True to do physical synchronization with the hardware or false to do only logical synchronization with the file system.off
- The offset of the region to be synchronized.size
- The size of the region to be synchronized. If it is zero, the length to the end of file is specified.- Returns:
- The result status.
- Note:
- The pysical file size can be larger than the logical size in order to improve performance by reducing frequency of allocation. Thus, you should call this function before accessing the file with external tools.
-
synchronize
Synchronizes the entire content of the file to the file system.- Parameters:
hard
- True to do physical synchronization with the hardware or false to do only logical synchronization with the file system.- Returns:
- The result status.
-
getSize
public long getSize()Gets the size of the file.- Returns:
- The size of the file or -1 on failure.
-
getPath
Gets the path of the file.- Returns:
- The path of the file or null on failure.
-
search
Searches the file and get lines which match a pattern.- Parameters:
mode
- The search mode. "contain" extracts keys containing the pattern. "begin" extracts keys beginning with the pattern. "end" extracts keys ending with the pattern. "regex" extracts keys partially matches the pattern of a regular expression. "edit" extracts keys whose edit distance to the UTF-8 pattern is the least. "editbin" extracts keys whose edit distance to the binary pattern is the least. "containcase", "containword", and "containcaseword" extract keys considering case and word boundary. Ordered databases support "upper" and "lower" which extract keys whose positions are upper/lower than the pattern. "upperinc" and "lowerinc" are their inclusive versions.pattern
- The pattern for matching.capacity
- The maximum records to obtain. 0 means unlimited.- Returns:
- An array of lines matching the condition.
-
search
Searches the file and get lines which match a pattern, with string data.- Parameters:
mode
- The search mode. "contain" extracts keys containing the pattern. "begin" extracts keys beginning with the pattern. "end" extracts keys ending with the pattern. "regex" extracts keys partially matches the pattern of a regular expression. "edit" extracts keys whose edit distance to the UTF-8 pattern is the least. "editbin" extracts keys whose edit distance to the binary pattern is the least. "containcase", "containword", and "containcaseword" extract keys considering case and word boundary. Ordered databases support "upper" and "lower" which extract keys whose positions are upper/lower than the pattern. "upperinc" and "lowerinc" are their inclusive versions.pattern
- The pattern for matching.capacity
- The maximum records to obtain. 0 means unlimited.- Returns:
- An array of lines matching the condition.
-
toString
Gets a string representation of the iterator.
-