public class RandomAccessFile extends Object implements RandomAccess
Constructor and Description |
---|
RandomAccessFile(File file,
String mode)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
int |
available()
Returns an estimate of the number of bytes that can be read.
|
void |
clear()
Clears all data of the buffer.
|
void |
close() |
long |
getPosition()
Returns offset of next byte to be returned by a read method.
|
boolean |
isClosed()
Returns true if this stream has been closed.
|
boolean |
isEOF()
A simple test to see if we are at the end of the data.
|
long |
length()
The total number of bytes that are available.
|
int |
peek()
This will peek at the next byte.
|
int |
read()
Read a single byte of data.
|
int |
read(byte[] b)
Read a buffer of data.
|
int |
read(byte[] b,
int offset,
int length)
Read a buffer of data.
|
byte[] |
readFully(int length)
Reads a given number of bytes.
|
void |
rewind(int bytes)
Seek backwards the given number of bytes.
|
void |
seek(long position)
Seek to a position in the data.
|
void |
write(byte[] b)
Write a buffer of data to the stream.
|
void |
write(byte[] b,
int offset,
int length)
Write a buffer of data to the stream.
|
void |
write(int b)
Write a byte to the stream.
|
public RandomAccessFile(File file, String mode) throws FileNotFoundException
file
- The file to write the data to.mode
- The writing mode.FileNotFoundException
- If the file cannot be created.public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
public void clear() throws IOException
RandomAccessWrite
clear
in interface RandomAccessWrite
IOException
public void seek(long position) throws IOException
RandomAccessRead
seek
in interface RandomAccessRead
position
- The position to seek to.IOException
- If there is an error while seeking.public long getPosition() throws IOException
RandomAccessRead
getPosition
in interface RandomAccessRead
RandomAccessRead.read()
(if no more bytes are left it returns a value >= length of source)IOException
public int read() throws IOException
RandomAccessRead
read
in interface RandomAccessRead
IOException
- If there is an error while reading the data.public int read(byte[] b) throws IOException
RandomAccessRead
read
in interface RandomAccessRead
b
- The buffer to write the data to.IOException
- If there was an error while reading the data.public int read(byte[] b, int offset, int length) throws IOException
RandomAccessRead
read
in interface RandomAccessRead
b
- The buffer to write the data to.offset
- Offset into the buffer to start writing.length
- The amount of data to attempt to read.IOException
- If there was an error while reading the data.public long length() throws IOException
RandomAccessRead
length
in interface RandomAccessRead
IOException
- If there is an IO error while determining the
length of the data stream.public boolean isClosed()
RandomAccessRead
isClosed
in interface RandomAccessRead
public void write(byte[] b, int offset, int length) throws IOException
RandomAccessWrite
write
in interface RandomAccessWrite
b
- The buffer to get the data from.offset
- An offset into the buffer to get the data from.length
- The length of data to write.IOException
- If there is an error while writing the data.public void write(byte[] b) throws IOException
RandomAccessWrite
write
in interface RandomAccessWrite
b
- The buffer to get the data from.IOException
- If there is an error while writing the data.public void write(int b) throws IOException
RandomAccessWrite
write
in interface RandomAccessWrite
b
- The byte to write.IOException
- If there is an IO error while writing.public int peek() throws IOException
RandomAccessRead
peek
in interface RandomAccessRead
IOException
- If there is an error reading the next byte.public void rewind(int bytes) throws IOException
RandomAccessRead
rewind
in interface RandomAccessRead
bytes
- the number of bytes to be seeked backwardsIOException
- If there is an error while seekingpublic byte[] readFully(int length) throws IOException
RandomAccessRead
readFully
in interface RandomAccessRead
length
- the number of bytes to be readIOException
- if an I/O error occurs while reading datapublic boolean isEOF() throws IOException
RandomAccessRead
isEOF
in interface RandomAccessRead
IOException
- If there is an error reading the next byte.public int available() throws IOException
RandomAccessRead
available
in interface RandomAccessRead
IOException
- if this random access has been closedCopyright © 2002–2016 The Apache Software Foundation. All rights reserved.