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?
RandomAccessFile
is meant for binary data. Although it can store and retrieve UTF-8 strings withwriteUTF/readUTF
, as you've found, itsreadLine
(and theDataInput
interface'sreadLine
in general) doesn't work on UTF-8. – RodomontadeRandomAccessFile
are given here. – Sd