I need to replace code using RandomAccessFile
with one that uses in-memory byte buffer (such as byte[]
or derivatives like ByteArrayInputStream
). Is there some API (byte array wrapper?) that has interface similar to RandomAccessFile
, with seek()
and streamish read()
which I could plug in one-to-one?
Java RandomAccessFile provides the facility to read and write data to a file. RandomAccessFile works with file as large array of bytes stored in the file system and a cursor using which we can move the file pointer position.
RandomAccessFile(File file, String mode) Creates a random access file stream to read from, and optionally to write to, the file specified by the File argument. RandomAccessFile(String name, String mode) Creates a random access file stream to read from, and optionally to write to, a file with the specified name.
readFloat() : java. io. RandomAccessFile. readFloat() reads a float value from the file, start reading from the File Pointer.
File is an abstract representation of a file/directory which may or may not even exist. It doesn't consume any resources, so you can store them as much as you want. RandomAccessFile is for actual file access (reading, seeking, writing), so you don't need it here.
ByteArrayInputStream
can do it:
read()
works the same.seek(n)
can be replaced with reset()
followed by skip(n)
May I suggest Java NIO (New I/O) check this simple and small tutorial
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With