Package biz.onixs.util
Class File
- java.lang.Object
-
- biz.onixs.util.File
-
public class File extends Object
A lightweight wrapper aroundRandomAccessFilethat provides convenience methods for reading, writing, and navigating file content using intuitive helper operations.
-
-
Field Summary
Fields Modifier and Type Field Description static intSEEK_CUROrigin constant used to move relative to the current file pointer.static intSEEK_ENDOrigin constant used to move relative to the end of file.static intSEEK_SETOrigin constant used to move relative to the beginning of file.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the underlying random access file if it is open.booleanendOfFile()Indicates whether the file pointer currently resides at end of file.voidflush(boolean metadata)Flushes file data to stable storage.intgetc()Reads the next byte from the file.FileChannelgetFileChannel()Provides direct access to the file channel backing this file wrapper.StringgetLine(int separator)Reads a line terminated by the provided separator.StringgetLine(int separator, int separator2)Reads a line terminated by either of the provided separators.voidgetLine(StringBuilder stringBuilder, int separator)Reads characters into the provided builder until the separator or EOF is reached.voidgetLine(StringBuilder stringBuilder, int separator, int separator2)Reads characters into the provided builder until one of the separators or EOF is reached.byte[]gets(byte[] string, int offset, int maxCount)Reads up tomaxCountbytes into the supplied array.Stringgets(int maxLength)Reads up tomaxLengthbytes and returns them as a string.voidignore(int nSymbols)Skips reading up tonSymbolsbytes, stopping at EOF.voidignore(int nSymbols, int delimiter)Skips reading up tonSymbolsbytes or until the delimiter is encountered.longlength()Retrieves the current length of the underlying file.booleanopen(String filePath, String mode)Opens the file at the specified path.Stringpath()Returns the path used to open this file.intread(byte[] buffer, int bufferLength)Reads up tobufferLengthbytes into the provided array.voidseek(long offset)Moves the file pointer to an absolute offset.voidseek(long offset, int origin)Moves the file pointer relative to the specified origin.longtell()Reports the current file pointer location.StringtoString()Returns a human-readable representation of the file wrapper.voidwrite(byte value)Writes a single byte value to the file.voidwrite(byte[] buf)Writes the provided buffer to the file.voidwrite(byte[] buf, int offset, int length)Writes a slice of the provided buffer to the file.voidwrite(ByteBuffer value)Writes the contents of the providedByteBuffer.voidwrite(String value)Writes the given string using the platform's default byte encoding.
-
-
-
Field Detail
-
SEEK_CUR
public static final int SEEK_CUR
Origin constant used to move relative to the current file pointer.- See Also:
- Constant Field Values
-
SEEK_END
public static final int SEEK_END
Origin constant used to move relative to the end of file.- See Also:
- Constant Field Values
-
SEEK_SET
public static final int SEEK_SET
Origin constant used to move relative to the beginning of file.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
File
public File()
Constructor.
-
File
public File(String filePath, String mode) throws FileNotFoundException
Constructor.- Parameters:
filePath- a pathname stringmode- the access mode- Throws:
FileNotFoundException- in case of file not found
-
-
Method Detail
-
close
public void close() throws IOExceptionCloses the underlying random access file if it is open.- Throws:
IOException- if an I/O error occurs while closing the file
-
getFileChannel
public FileChannel getFileChannel()
Provides direct access to the file channel backing this file wrapper.- Returns:
- the
FileChannelassociated with the underlying file
-
length
public long length() throws IOExceptionRetrieves the current length of the underlying file.- Returns:
- the file length in bytes
- Throws:
IOException- if the length cannot be read
-
endOfFile
public boolean endOfFile()
Indicates whether the file pointer currently resides at end of file.- Returns:
truewhen the pointer is at EOF or an error occurs;falseotherwise
-
path
public String path()
Returns the path used to open this file.- Returns:
- the file path, or
nullif the file has not been opened
-
getc
public int getc()
Reads the next byte from the file.- Returns:
- the unsigned byte value in the range 0-255, or
EOFon failure or end of file
-
getLine
public void getLine(StringBuilder stringBuilder, int separator) throws IOException
Reads characters into the provided builder until the separator or EOF is reached.- Parameters:
stringBuilder- the destination for the characters that are readseparator- the byte value that stops the read- Throws:
IOException- if the read fails
-
getLine
public String getLine(int separator) throws IOException
Reads a line terminated by the provided separator.- Parameters:
separator- the byte value that terminates the line- Returns:
- the collected characters (without the separator)
- Throws:
IOException- if an I/O error occurs
-
getLine
public void getLine(StringBuilder stringBuilder, int separator, int separator2) throws IOException
Reads characters into the provided builder until one of the separators or EOF is reached.- Parameters:
stringBuilder- the destination for charactersseparator- the primary terminating characterseparator2- an alternative terminating character- Throws:
IOException- if reading fails
-
getLine
public String getLine(int separator, int separator2) throws IOException
Reads a line terminated by either of the provided separators.- Parameters:
separator- the primary terminating characterseparator2- an alternative terminating character- Returns:
- the collected characters without the separator byte
- Throws:
IOException- if an I/O error occurs
-
gets
public byte[] gets(byte[] string, int offset, int maxCount)Reads up tomaxCountbytes into the supplied array.- Parameters:
string- the destination bufferoffset- the starting offset in the destination buffermaxCount- the maximum number of bytes to read- Returns:
- the destination buffer, or
nullwhen EOF is reached or an error occurs
-
gets
public String gets(int maxLength)
Reads up tomaxLengthbytes and returns them as a string.- Parameters:
maxLength- the maximum number of bytes to read- Returns:
- a string containing the bytes read, or
nullwhen EOF is reached or an error occurs
-
ignore
public void ignore(int nSymbols, int delimiter)Skips reading up tonSymbolsbytes or until the delimiter is encountered.- Parameters:
nSymbols- the maximum number of bytes to skipdelimiter- the terminating byte value; skipping stops when this is read
-
ignore
public void ignore(int nSymbols)
Skips reading up tonSymbolsbytes, stopping at EOF.- Parameters:
nSymbols- the maximum number of bytes to skip
-
flush
public void flush(boolean metadata) throws IOExceptionFlushes file data to stable storage.
Whenmetadataistrue, both file data and metadata (e.g., file size) are forced to disk. Whenfalse, only file data is flushed.- Parameters:
metadata- whether file metadata should also be flushed- Throws:
IOException- if the flush operation fails
-
open
public boolean open(String filePath, String mode)
Opens the file at the specified path.- Parameters:
filePath- the target file pathmode- the access mode used byRandomAccessFile- Returns:
truewhen the file is opened successfully;falseotherwise
-
read
public int read(byte[] buffer, int bufferLength) throws IOExceptionReads up tobufferLengthbytes into the provided array.- Parameters:
buffer- the destination bufferbufferLength- the maximum number of bytes to read- Returns:
- the number of bytes read, or
-1at EOF - Throws:
IOException- if the read fails
-
seek
public void seek(long offset) throws IOExceptionMoves the file pointer to an absolute offset.- Parameters:
offset- the absolute position to move to- Throws:
IOException- if the seek fails
-
seek
public void seek(long offset, int origin) throws IOExceptionMoves the file pointer relative to the specified origin.- Parameters:
offset- the number of bytes to moveorigin- one ofSEEK_SET,SEEK_CUR, orSEEK_END- Throws:
IOException- if the seek fails
-
tell
public long tell() throws IOExceptionReports the current file pointer location.- Returns:
- the zero-based offset of the file pointer
- Throws:
IOException- if the position cannot be obtained
-
write
public void write(byte value) throws IOExceptionWrites a single byte value to the file.- Parameters:
value- the byte to write- Throws:
IOException- if the write fails
-
write
public void write(byte[] buf) throws IOExceptionWrites the provided buffer to the file.- Parameters:
buf- the bytes to write- Throws:
IOException- if the write fails
-
write
public void write(ByteBuffer value) throws IOException
Writes the contents of the providedByteBuffer.- Parameters:
value- the buffer containing data to write- Throws:
IOException- if the write fails
-
write
public void write(String value) throws IOException
Writes the given string using the platform's default byte encoding.- Parameters:
value- the string to write- Throws:
IOException- if the write fails
-
write
public void write(byte[] buf, int offset, int length) throws IOExceptionWrites a slice of the provided buffer to the file.- Parameters:
buf- the source bufferoffset- the starting index in the bufferlength- the number of bytes to write- Throws:
IOException- if the write fails
-
-