- All Superinterfaces:
AutoCloseable, Closeable
- All Known Implementing Classes:
RAIFile
Interface for random access file operations. Provides methods for reading, writing, and seeking within files.
Method Summary
All MethodsModifier and TypeMethodDescriptionbooleancanWrite()Is the file writable? (I2P) Only valid if the File constructor was used, not the RAF constructorvoidclose()Closes the file.longReturns the current position of the file pointer.longlength()Returns the size of the file in bytes.intread()Reads one byte and advances the file pointer past it.intread(byte[] b) Reads bytes into b and advances the file pointer past them.intread(byte[] b, int off, int len) Reads bytes into b at off and advances the file pointer past them.booleanReads a single byte, reporting it as a boolean.bytereadByte()Reads one byte as a signed value.charreadChar()Reads two bytes as a big-endian char.doubleReads eight bytes as a big-endian double.floatReads four bytes as a big-endian float.voidreadFully(byte[] b) Fills b, failing unless every byte of it is read.voidreadFully(byte[] b, int off, int len) Fills len bytes of b at off, failing unless all are read.intreadInt()Reads four bytes as a big-endian int.readLine()Reads bytes up to a line terminator, discarding the terminator.longreadLong()Reads eight bytes as a big-endian long.shortReads two bytes as a big-endian short.intReads one byte, widening it to an unsigned int.intRead a 4-byte big-endian unsigned int.intReads two bytes, widening them to an unsigned int.readUTF()Reads a length-prefixed UTF-8 string.voidseek(long pos) Moves the file pointer to an absolute offset.voidsetLength(long newLength) Truncates or extends the file to the given size.intskipBytes(int n) Advances the file pointer without reading the bytes skipped.voidwrite(byte[] b) Writes every byte of b.voidwrite(byte[] b, int off, int len) Writes len bytes from b, starting at off.voidwrite(int b) Writes the low eight bits of b.voidwriteBoolean(boolean v) Writes a single byte holding 1 for true, 0 for false.voidwriteByte(int v) Writes the low eight bits of v.voidwriteBytes(String s) Writes the low byte of each character of s.voidwriteChar(int v) Writes the low sixteen bits of v as a big-endian char.voidwriteChars(String s) Writes each character of s as two big-endian bytes.voidwriteDouble(double v) Writes v as eight big-endian bytes.voidwriteFloat(float v) Writes v as four big-endian bytes.voidwriteInt(int v) Writes v as four big-endian bytes.voidwriteLong(long v) Writes v as eight big-endian bytes.voidwriteShort(int v) Writes the low sixteen bits of v as a big-endian short.voidWrites str as a length-prefixed UTF-8 string.
Method Details
getFilePointer
Returns the current position of the file pointer.- Returns:
- the file pointer
- Throws:
IOException- if the pointer cannot be determined
length
Returns the size of the file in bytes.- Returns:
- the file length
- Throws:
IOException- if the length cannot be determined
read
Reads one byte and advances the file pointer past it.- Returns:
- the byte read, or -1 at the end of file
- Throws:
IOException- if the file cannot be read- See Also:
read
Reads bytes into b and advances the file pointer past them.- Parameters:
b- the buffer to fill- Returns:
- the number of bytes read, or -1 at the end of file
- Throws:
IOException- if the file cannot be read- See Also:
read
Reads bytes into b at off and advances the file pointer past them.- Parameters:
b- the buffer to filloff- the index in b at which to start writinglen- the maximum number of bytes to read- Returns:
- the number of bytes read, or -1 at the end of file
- Throws:
IOException- if the file cannot be read- See Also:
seek
Moves the file pointer to an absolute offset.- Parameters:
pos- the absolute offset, in bytes, to position at- Throws:
IOException- if pos is negative or the seek fails- See Also:
setLength
Truncates or extends the file to the given size.- Parameters:
newLength- the size, in bytes, the file should have- Throws:
IOException- if the file cannot be resized- See Also:
canWrite
boolean canWrite()Is the file writable? (I2P) Only valid if the File constructor was used, not the RAF constructor- Returns:
- true if the file was opened for writing
close
Closes the file.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- See Also:
readBoolean
Reads a single byte, reporting it as a boolean.- Returns:
- true if the byte read is nonzero
- Throws:
IOException- if the file cannot be read- See Also:
readByte
Reads one byte as a signed value.- Returns:
- the byte read
- Throws:
IOException- if the file cannot be read- See Also:
readChar
Reads two bytes as a big-endian char.- Returns:
- the char read
- Throws:
IOException- if the file cannot be read- See Also:
readDouble
Reads eight bytes as a big-endian double.- Returns:
- the double read
- Throws:
IOException- if the file cannot be read- See Also:
readFloat
Reads four bytes as a big-endian float.- Returns:
- the float read
- Throws:
IOException- if the file cannot be read- See Also:
readFully
Fills b, failing unless every byte of it is read.- Parameters:
b- the buffer to fill- Throws:
IOException- if the read fails or the end of file comes first- See Also:
readFully
Fills len bytes of b at off, failing unless all are read.- Parameters:
b- the buffer to filloff- the index in b at which to start writinglen- the number of bytes required- Throws:
IOException- if the read fails or the end of file comes first- See Also:
readInt
Reads four bytes as a big-endian int.- Returns:
- the int read
- Throws:
IOException- if the file cannot be read- See Also:
readLine
Reads bytes up to a line terminator, discarding the terminator.- Returns:
- the line read, or null at the end of file
- Throws:
IOException- if the file cannot be read- See Also:
readLong
Reads eight bytes as a big-endian long.- Returns:
- the long read
- Throws:
IOException- if the file cannot be read- See Also:
readShort
Reads two bytes as a big-endian short.- Returns:
- the short read
- Throws:
IOException- if the file cannot be read- See Also:
readUnsignedByte
Reads one byte, widening it to an unsigned int.- Returns:
- the byte read, 0 to 255
- Throws:
IOException- if the file cannot be read- See Also:
readUnsignedShort
Reads two bytes, widening them to an unsigned int.- Returns:
- the short read, 0 to 65535
- Throws:
IOException- if the file cannot be read- See Also:
readUnsignedInt
Read a 4-byte big-endian unsigned int.- Returns:
- the value read, which must be non-negative
- Throws:
IOException- if the value read is negative
readUTF
Reads a length-prefixed UTF-8 string.- Returns:
- the decoded string
- Throws:
IOException- if the length prefix or the string is malformed- See Also:
skipBytes
Advances the file pointer without reading the bytes skipped.- Parameters:
n- the number of bytes to skip- Returns:
- the number of bytes actually skipped
- Throws:
IOException- if the file cannot be read- See Also:
write
Writes the low eight bits of b.- Parameters:
b- the value to write, of which only the low byte is used- Throws:
IOException- if the file cannot be written- See Also:
write
Writes every byte of b.- Parameters:
b- the bytes to write- Throws:
IOException- if the file cannot be written- See Also:
write
Writes len bytes from b, starting at off.- Parameters:
b- the buffer the bytes are taken fromoff- the index in b at which to start readinglen- the number of bytes to write- Throws:
IOException- if the file cannot be written- See Also:
writeBoolean
Writes a single byte holding 1 for true, 0 for false.- Parameters:
v- the boolean to write- Throws:
IOException- if the file cannot be written- See Also:
writeByte
Writes the low eight bits of v.- Parameters:
v- the value to write, truncated to eight bits- Throws:
IOException- if the file cannot be written- See Also:
writeShort
Writes the low sixteen bits of v as a big-endian short.- Parameters:
v- the value to write, truncated to sixteen bits- Throws:
IOException- if the file cannot be written- See Also:
writeChar
Writes the low sixteen bits of v as a big-endian char.- Parameters:
v- the value to write, truncated to sixteen bits- Throws:
IOException- if the file cannot be written- See Also:
writeInt
Writes v as four big-endian bytes.- Parameters:
v- the value to write- Throws:
IOException- if the file cannot be written- See Also:
writeLong
Writes v as eight big-endian bytes.- Parameters:
v- the value to write- Throws:
IOException- if the file cannot be written- See Also:
writeFloat
Writes v as four big-endian bytes.- Parameters:
v- the value to write- Throws:
IOException- if the file cannot be written- See Also:
writeDouble
Writes v as eight big-endian bytes.- Parameters:
v- the value to write- Throws:
IOException- if the file cannot be written- See Also:
writeBytes
Writes the low byte of each character of s.- Parameters:
s- the string whose characters are written, one byte each- Throws:
IOException- if the file cannot be written- See Also:
writeChars
Writes each character of s as two big-endian bytes.- Parameters:
s- the string to write- Throws:
IOException- if the file cannot be written- See Also:
writeUTF
Writes str as a length-prefixed UTF-8 string.- Parameters:
str- the string to write- Throws:
IOException- if str is too long to encode- See Also: