I've never had close experiences with Java IO API before and I'm really frustrated now. I find it hard to believe how strange and complex it is and how hard it could be to do a simple task.
My task: I have 2 positions (starting byte, ending byte), pos1
and pos2
. I need to read lines between these two bytes (including the starting one, not including the ending one) and use them as UTF8 String objects.
For example, in most script languages it would be a very simple 1-2-3-liner like that (in Ruby, but it will be essentially the same for Python, Perl, etc):
f = File.open("file.txt").seek(pos1)
while f.pos < pos2 {
s = f.readline
# do something with "s" here
}
It quickly comes hell with Java IO APIs ;) In fact, I see two ways to read lines (ending with \n
) from regular local files:
- RandomAccessFile has
getFilePointer()
andseek(long pos)
, but it's readLine() reads non-UTF8 strings (and even not byte arrays), but very strange strings with broken encoding, and it has no buffering (which probably means that everyread*()
call would be translated into single undelying OSread()
=> fairly slow). - BufferedReader has great
readLine()
method, and it can even do some seeking withskip(long n)
, but it has no way to determine even number of bytes that has been already read, not mentioning the current position in a file.
I've tried to use something like:
FileInputStream fis = new FileInputStream(fileName);
FileChannel fc = fis.getChannel();
BufferedReader br = new BufferedReader(
new InputStreamReader(
fis,
CHARSET_UTF8
)
);
... and then using fc.position()
to get current file reading position and fc.position(newPosition)
to set one, but it doesn't seem to work in my case: looks like it returns position of a buffer pre-filling done by BufferedReader, or something like that - these counters seem to be rounded up in 16K increments.
Do I really have to implement it all by myself, i.e. a file readering interface which would:
- allow me to get/set position in a file
- buffer file reading operations
- allow reading UTF8 strings (or at least allow operations like "read everything till the next
\n
")
Is there a quicker way than implementing it all myself? Am I overseeing something?
Start with a
RandomAccessFile
and useread
orreadFully
to get a byte array betweenpos1
andpos2
. Let's say that we've stored the data read in a variable namedrawBytes
.Then create your
BufferedReader
usingThen you can call
readLine
on theBufferedReader
.Caveat: this probably uses more memory than if you could make the
BufferedReader
seek to the right location itself, because it preloads everything into memory.