Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. import java.io.FileNotFoundException;
  2. import java.io.IOException;
  3. import java.io.RandomAccessFile;
  4.  
  5.  
  6. public class ReverseReader {
  7.  
  8.     private static final int LINES = 100;
  9.     private static final String FILENAME = "C:\\Users\\KowKiller\\Dropbox\\mIRC\\logs\\#superamici.synIRC.log";
  10.  
  11.     public static void main(String[] args) {
  12.         RandomAccessFile stream = null;
  13.         try {
  14.             stream = new RandomAccessFile(FILENAME, "r");
  15.             long pos = stream.length();
  16.             safeSeek(stream, --pos);
  17.             int c;
  18.             int nLines = 0;
  19.             while (pos >= 0 && nLines < LINES + 1 && (c = stream.read()) != -1) {
  20.                 if (c == '\n') {
  21.                     nLines++;
  22.                 }
  23.                 safeSeek(stream, --pos);
  24.             }
  25.             stream.seek(pos+2);
  26.             int i=0;
  27.             String line;
  28.             while ((line = stream.readLine()) != null) {
  29.                 System.out.println(line);
  30.             }
  31.  
  32.         } catch (FileNotFoundException e) {
  33.             e.printStackTrace();
  34.         } catch (IOException e) {
  35.             e.printStackTrace();
  36.         } finally {
  37.             try {
  38.                 stream.close();
  39.             } catch (IOException e) {
  40.                 e.printStackTrace();
  41.             }
  42.         }
  43.        
  44.  
  45.     }
  46.  
  47.     private static void safeSeek(RandomAccessFile stream, long pos)
  48.             throws IOException {
  49.         if (pos > 0) {
  50.             stream.seek(pos);
  51.         } else {
  52.             stream.seek(0);
  53.         }
  54.     }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement