public class File extends Object
RandomAccessFile that provides
convenience methods for reading, writing, and navigating file content
using intuitive helper operations.| Modifier and Type | Field and Description |
|---|---|
static int |
SEEK_CUR
Origin constant used to move relative to the current file pointer.
|
static int |
SEEK_END
Origin constant used to move relative to the end of file.
|
static int |
SEEK_SET
Origin constant used to move relative to the beginning of file.
|
| Constructor and Description |
|---|
File()
Constructor.
|
File(String filePath,
String mode)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes the underlying random access file if it is open.
|
boolean |
endOfFile()
Indicates whether the file pointer currently resides at end of file.
|
void |
flush()
Forces the operating system to flush the file descriptor to storage.
|
int |
getc()
Reads the next byte from the file.
|
FileChannel |
getFileChannel()
Provides direct access to the file channel backing this file wrapper.
|
String |
getLine(int separator)
Reads a line terminated by the provided separator.
|
String |
getLine(int separator,
int separator2)
Reads a line terminated by either of the provided separators.
|
void |
getLine(StringBuilder stringBuilder,
int separator)
Reads characters into the provided builder until the separator or EOF is reached.
|
void |
getLine(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 to
maxCount bytes into the supplied array. |
String |
gets(int maxLength)
Reads up to
maxLength bytes and returns them as a string. |
void |
ignore(int nSymbols)
Skips reading up to
nSymbols bytes, stopping at EOF. |
void |
ignore(int nSymbols,
int delimiter)
Skips reading up to
nSymbols bytes or until the delimiter is encountered. |
long |
length()
Retrieves the current length of the underlying file.
|
boolean |
open(String filePath,
String mode)
Opens the file at the specified path.
|
String |
path()
Returns the path used to open this file.
|
int |
read(byte[] buffer,
int bufferLength)
Reads up to
bufferLength bytes into the provided array. |
void |
seek(long offset)
Moves the file pointer to an absolute offset.
|
void |
seek(long offset,
int origin)
Moves the file pointer relative to the specified origin.
|
long |
tell()
Reports the current file pointer location.
|
String |
toString()
Returns a human-readable representation of the file wrapper.
|
void |
write(byte value)
Writes a single byte value to the file.
|
void |
write(byte[] buf)
Writes the provided buffer to the file.
|
void |
write(byte[] buf,
int offset,
int length)
Writes a slice of the provided buffer to the file.
|
void |
write(ByteBuffer value)
Writes the contents of the provided
ByteBuffer. |
void |
write(String value)
Writes the given string using the platform's default byte encoding.
|
public static final int SEEK_CUR
public static final int SEEK_END
public static final int SEEK_SET
public File()
public File(String filePath, String mode) throws FileNotFoundException
filePath - a pathname stringmode - the access modeFileNotFoundException - in case of file not foundpublic void close()
throws IOException
IOException - if an I/O error occurs while closing the filepublic FileChannel getFileChannel()
FileChannel associated with the underlying filepublic long length()
throws IOException
IOException - if the length cannot be readpublic boolean endOfFile()
true when the pointer is at EOF or an error occurs; false otherwisepublic String path()
null if the file has not been openedpublic int getc()
EOF on failure or end of filepublic void getLine(StringBuilder stringBuilder, int separator) throws IOException
stringBuilder - the destination for the characters that are readseparator - the byte value that stops the readIOException - if the read failspublic String getLine(int separator) throws IOException
separator - the byte value that terminates the lineIOException - if an I/O error occurspublic void getLine(StringBuilder stringBuilder, int separator, int separator2) throws IOException
stringBuilder - the destination for charactersseparator - the primary terminating characterseparator2 - an alternative terminating characterIOException - if reading failspublic String getLine(int separator, int separator2) throws IOException
separator - the primary terminating characterseparator2 - an alternative terminating characterIOException - if an I/O error occurspublic byte[] gets(byte[] string,
int offset,
int maxCount)
maxCount bytes into the supplied array.string - the destination bufferoffset - the starting offset in the destination buffermaxCount - the maximum number of bytes to readnull when EOF is reached or an error occurspublic String gets(int maxLength)
maxLength bytes and returns them as a string.maxLength - the maximum number of bytes to readnull when EOF is reached or an error occurspublic void ignore(int nSymbols,
int delimiter)
nSymbols bytes or until the delimiter is encountered.nSymbols - the maximum number of bytes to skipdelimiter - the terminating byte value; skipping stops when this is readpublic void ignore(int nSymbols)
nSymbols bytes, stopping at EOF.nSymbols - the maximum number of bytes to skippublic void flush()
throws IOException
IOException - if the sync operation failspublic boolean open(String filePath, String mode)
filePath - the target file pathmode - the access mode used by RandomAccessFiletrue when the file is opened successfully; false otherwisepublic int read(byte[] buffer,
int bufferLength)
throws IOException
bufferLength bytes into the provided array.buffer - the destination bufferbufferLength - the maximum number of bytes to read-1 at EOFIOException - if the read failspublic void seek(long offset)
throws IOException
offset - the absolute position to move toIOException - if the seek failspublic void seek(long offset,
int origin)
throws IOException
offset - the number of bytes to moveorigin - one of SEEK_SET, SEEK_CUR, or SEEK_ENDIOException - if the seek failspublic long tell()
throws IOException
IOException - if the position cannot be obtainedpublic void write(byte value)
throws IOException
value - the byte to writeIOException - if the write failspublic void write(byte[] buf)
throws IOException
buf - the bytes to writeIOException - if the write failspublic void write(ByteBuffer value) throws IOException
ByteBuffer.value - the buffer containing data to writeIOException - if the write failspublic void write(String value) throws IOException
value - the string to writeIOException - if the write failspublic void write(byte[] buf,
int offset,
int length)
throws IOException
buf - the source bufferoffset - the starting index in the bufferlength - the number of bytes to writeIOException - if the write failsCopyright © 2005–2025 Onix Solutions. All rights reserved.