Last active
August 29, 2015 14:14
-
-
Save kineticz/79c040ae83c95b87520a to your computer and use it in GitHub Desktop.
Why do I get a null point exception from this?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // finally got it right | |
| package je3.io; | |
| import java.io.IOException; | |
| import java.io.RandomAccessFile; | |
| /** | |
| * Created by IDEA on 29/01/15. | |
| */ | |
| public class Tail { | |
| public static String[] tailLines(String filename, int nLinesToRead) throws IOException { | |
| RandomAccessFile randomAccessFile = new RandomAccessFile(filename, "r"); | |
| long lengthOfFile = randomAccessFile.length(); | |
| long counterFromEnd = 1L; | |
| long newlineCounterGoal = nLinesToRead; | |
| int newlineCounter = 0; | |
| long tailPosition = 0L; // start of the end ;-) | |
| // If you want to get the last 10 lines, | |
| // and the last line ends with a newline, then you need to count back 11 newlines | |
| // if there is no trailing newline, then you only need to count back 10 | |
| randomAccessFile.seek(lengthOfFile - 1L); | |
| char currentChar = (char) randomAccessFile.readByte(); | |
| if(currentChar == '\n') { | |
| newlineCounterGoal++; | |
| } | |
| while(counterFromEnd <= lengthOfFile) { | |
| randomAccessFile.seek(lengthOfFile - counterFromEnd); | |
| if(randomAccessFile.readByte() == '\n') { | |
| newlineCounter++; | |
| } | |
| if(newlineCounter == newlineCounterGoal) { | |
| tailPosition = randomAccessFile.getFilePointer(); | |
| break; | |
| } | |
| counterFromEnd++; | |
| } | |
| randomAccessFile.seek(tailPosition); | |
| String line; | |
| String[] lines = new String[nLinesToRead]; | |
| int nLine = 0; | |
| while((line = randomAccessFile.readLine()) != null) { | |
| lines[nLine++] = line; | |
| } | |
| return lines; | |
| } | |
| public static void main(String[] args) throws IOException { | |
| if(args.length != 1) { | |
| throw new IllegalArgumentException(); | |
| } | |
| String[] lines = tailLines(args[0], 10); | |
| for(String line : lines) { | |
| if(line != null) { | |
| System.out.println(line); | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package je3.io; | |
| import java.io.IOException; | |
| import java.io.RandomAccessFile; | |
| /** | |
| * Created by IDEA on 29/01/15. | |
| */ | |
| public class Tail { | |
| public static String[] tailLines(String filename) throws IOException { | |
| RandomAccessFile randomAccessFile = new RandomAccessFile(filename, "r"); | |
| long nTotalLines = randomAccessFile.length(); | |
| if(nTotalLines > 10) { | |
| randomAccessFile.seek(nTotalLines - 10); | |
| } | |
| String line; | |
| String[] lines = new String[10]; | |
| int nLine = 0; | |
| while((line = randomAccessFile.readLine()) != null) { | |
| lines[nLine++] = line; | |
| } | |
| return lines; | |
| } | |
| public static void main(String[] args) throws IOException { | |
| if(args.length != 1) { | |
| throw new IllegalArgumentException(); | |
| } | |
| String[] lines = tailLines(args[0]); | |
| for(String line : lines) { | |
| if(!line.isEmpty()) { | |
| System.out.println(line); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment