Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Neil Coffey's solution is good if you are reading fixed length files. However for files that have variable length (data keep coming in) there are some problems with using BufferedReader directly on FileInputStream or FileChannel inputstream via an InputStreamReader. For ex consider the cases</p> <ul> <li><p>1) You want to read data from some offset to current file length. So you use BR on FileInputStream/FileChannel(via an InputStreamReader) and use its readLine method. But while you are busy reading the data let say some data got added which causes BF's readLine to read more data than what you expected(the previous file length)</p></li> <li><p>2) You finished readLine stuff but when you try to read the current file length/channel position some data got added suddenly which causes the current file length/channel position to increase but you have already read less data than this.</p></li> </ul> <p>In both of the above cases it is difficult to know the actual data you have read (you cannot just use the length of data read using readLine because it skips some chars like carriage return)</p> <p>So it is better to read the data in buffered bytes and use a BufferedReader wrapper around this. I wrote some methods like this</p> <pre><code>/** Read data from offset to length bytes in RandomAccessFile using BufferedReader * @param offset * @param length * @param accessFile * @throws IOException */ public static void readBufferedLines(long offset, long length, RandomAccessFile accessFile) throws IOException{ if(accessFile == null) return; int bufferSize = BYTE_BUFFER_SIZE;// constant say 4096 if(offset &lt; length &amp;&amp; offset &gt;= 0){ int index = 1; long curPosition = offset; /* * iterate (length-from)/BYTE_BUFFER_SIZE times to read into buffer no matter where new line occurs */ while((curPosition + (index * BYTE_BUFFER_SIZE)) &lt; length){ accessFile.seek(offset); // seek to last parsed data rather than last data read in to buffer byte[] buf = new byte[bufferSize]; int read = accessFile.read(buf, 0, bufferSize); index++;// Increment whether or not read successful if(read &gt; 0){ int lastnewLine = getLastLine(read,buf); if(lastnewLine &lt;= 0){ // no new line found in the buffer reset buffer size and continue bufferSize = bufferSize+read; continue; } else{ bufferSize = BYTE_BUFFER_SIZE; } readLine(buf, 0, lastnewLine); // read the lines from buffer and parse the line offset = offset+lastnewLine; // update the last data read } } // Read last chunk. The last chunk size in worst case is the total file when no newline occurs if(offset &lt; length){ accessFile.seek(offset); byte[] buf = new byte[(int) (length-offset)]; int read = accessFile.read(buf, 0, buf.length); if(read &gt; 0){ readLine(buf, 0, read); offset = offset+read; // update the last data read } } } } private static void readLine(byte[] buf, int from , int lastnewLine) throws IOException{ String readLine = ""; BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(buf,from,lastnewLine) )); while( (readLine = reader.readLine()) != null){ //do something with readLine System.out.println(readLine); } reader.close(); } private static int getLastLine(int read, byte[] buf) { if(buf == null ) return -1; if(read &gt; buf.length) read = buf.length; while( read &gt; 0 &amp;&amp; !(buf[read-1] == '\n' || buf[read-1] == '\r')) read--; return read; } public static void main(String[] args) throws IOException { RandomAccessFile accessFile = new RandomAccessFile("C:/sri/test.log", "r"); readBufferedLines(0, accessFile.length(), accessFile); accessFile.close(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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