Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: reading strings from a random access file with buffered input
    primarykey
    data
    text
    <p>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.</p> <p>My task: I have 2 positions (starting byte, ending byte), <code>pos1</code> and <code>pos2</code>. 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.</p> <p>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):</p> <pre><code>f = File.open("file.txt").seek(pos1) while f.pos &lt; pos2 { s = f.readline # do something with "s" here } </code></pre> <p>It quickly comes hell with Java IO APIs ;) In fact, I see two ways to read lines (ending with <code>\n</code>) from regular local files:</p> <ul> <li><a href="http://download.oracle.com/javase/6/docs/api/java/io/RandomAccessFile.html" rel="noreferrer">RandomAccessFile</a> has <code>getFilePointer()</code> and <code>seek(long pos)</code>, but it's <a href="http://download.oracle.com/javase/6/docs/api/java/io/RandomAccessFile.html#readLine()" rel="noreferrer">readLine()</a> 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 every <code>read*()</code> call would be translated into single undelying OS <code>read()</code> => fairly slow).</li> <li><a href="http://download.oracle.com/javase/6/docs/api/java/io/BufferedReader.html" rel="noreferrer">BufferedReader</a> has great <code>readLine()</code> method, and it can even do some seeking with <code>skip(long n)</code>, but it has no way to determine even number of bytes that has been already read, not mentioning the current position in a file.</li> </ul> <p>I've tried to use something like:</p> <pre><code> FileInputStream fis = new FileInputStream(fileName); FileChannel fc = fis.getChannel(); BufferedReader br = new BufferedReader( new InputStreamReader( fis, CHARSET_UTF8 ) ); </code></pre> <p>... and then using <code>fc.position()</code> to get current file reading position and <code>fc.position(newPosition)</code> 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.</p> <p>Do I really have to implement it all by myself, i.e. a file readering interface which would:</p> <ul> <li>allow me to get/set position in a file</li> <li>buffer file reading operations</li> <li>allow reading UTF8 strings (or at least allow operations like "read everything till the next <code>\n</code>")</li> </ul> <p>Is there a quicker way than implementing it all myself? Am I overseeing something?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload