Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- public class ReverseReader {
- private static final int LINES = 100;
- private static final String FILENAME = "C:\\Users\\KowKiller\\Dropbox\\mIRC\\logs\\#superamici.synIRC.log";
- public static void main(String[] args) {
- RandomAccessFile stream = null;
- try {
- stream = new RandomAccessFile(FILENAME, "r");
- long pos = stream.length();
- safeSeek(stream, --pos);
- int c;
- int nLines = 0;
- while (pos >= 0 && nLines < LINES + 1 && (c = stream.read()) != -1) {
- if (c == '\n') {
- nLines++;
- }
- safeSeek(stream, --pos);
- }
- stream.seek(pos+2);
- int i=0;
- String line;
- while ((line = stream.readLine()) != null) {
- System.out.println(line);
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- stream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- private static void safeSeek(RandomAccessFile stream, long pos)
- throws IOException {
- if (pos > 0) {
- stream.seek(pos);
- } else {
- stream.seek(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement