Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>By taking RandomAccessFile you can:</p> <ul> <li>use method <strong>seek(long)</strong> to jump forward and read those lines. But you won't know exactly how big the jump should be.</li> <li>to delete last lines you need the position of begin of last line so before reading each line store their file pointer position (method <strong>getFilePointer()</strong>). Deleting to that position you use <strong>setLength(long)</strong>.</li> </ul> <p>Code would be something like this:</p> <pre><code> LinkedList&lt;String&gt; lines=null; int howMuch = 1; // one line to read and delete try{ RandomAccessFile raf = new RandomAccessFile(inputFileName, "rw"); System.out.println("File Length="+raf.length()); long step = 20; // here I advice to write average length of line long jump = raf.length()&lt;step*howMuch? 0: raf.length()-step*howMuch; raf.seek(jump); lines = new LinkedList&lt;String&gt;(); LinkedList&lt;Long&gt; pos = new LinkedList&lt;Long&gt;(); Entry&lt;LinkedList&lt;String&gt;,LinkedList&lt;Long&gt;&gt; rLinesRead = getRemainingLines(raf, new AbstractMap.SimpleEntry&lt;LinkedList&lt;String&gt;,LinkedList&lt;Long&gt;&gt; (lines,pos)); while(rLinesRead.getKey().size()&lt;howMuch){ if(jump&lt;step) if(jump&lt;=0) break; else{ jump=0; raf.seek(jump); rLinesRead=getRemainingLines(raf,rLinesRead); break; } else jump=jump-step; raf.seek(jump); rLinesRead=getRemainingLines(raf,rLinesRead); } int originalSize=rLinesRead.getKey().size(); lines=rLinesRead.getKey(); pos=rLinesRead.getValue(); for (int i=0;i&lt;originalSize-howMuch;++i){ lines.removeFirst(); pos.removeFirst(); } if(!pos.isEmpty()) raf.setLength(pos.getFirst()); // before last(from EOF) returned fully read lines in file }catch(Exception ex){ ex.printStackTrace(); } finally{ try { raf.close(); } catch (Exception e) { e.printStackTrace(); } } //returns line to EOF with their begin file pointer positions private Entry&lt;LinkedList&lt;String&gt;,LinkedList&lt;Long&gt;&gt; getRemainingLines(RandomAccessFile raf, Entry&lt;LinkedList&lt;String&gt;,LinkedList&lt;Long&gt;&gt; linesAlreadyLoadedFromEnd) throws IOException{ LinkedList&lt;String&gt; pLines = linesAlreadyLoadedFromEnd.getKey(); LinkedList&lt;Long&gt; pPos = linesAlreadyLoadedFromEnd.getValue(); long init=raf.getFilePointer(); String str = raf.readLine(); if(pPos.size()&gt;0?pPos.getFirst()==0:false || str==null) return linesAlreadyLoadedFromEnd; LinkedList&lt;String&gt; lines = new LinkedList&lt;String&gt;(); LinkedList&lt;Long&gt; pos = new LinkedList&lt;Long&gt;(); if(init==0L ){ lines.add(str); pos.add(0L); } Long tmpPos = raf.getFilePointer(); while ((str = raf.readLine())!=null &amp;&amp; !pPos.contains(tmpPos)){ lines.add(str); pos.add(tmpPos); tmpPos = raf.getFilePointer(); } pLines.addAll(0,lines); pPos.addAll(0,pos); return new AbstractMap.SimpleEntry&lt;LinkedList&lt;String&gt;,LinkedList&lt;Long&gt;&gt; (pLines,pPos); } </code></pre>
 

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