Tail function in JAVA. Get the last lines of a given file.
/**
* Get the last lines of the file
**/
public String tail(File file, int lines) {
java.io.RandomAccessFile fileHandler = null;
try {
fileHandler = new java.io.RandomAccessFile(file, "r");
long fileLength = fileHandler.length() - 1;
StringBuilder sb = new StringBuilder();
int line = 0;
for (long filePointer = fileLength; filePointer != -1; filePointer--) {
fileHandler.seek(filePointer);
int readByte = fileHandler.readByte();
if (readByte == 0xA) {
line = line + 1;
if (line == lines) {
if (filePointer == fileLength) {
continue;
}
break;
}
} else if (readByte == 0xD) {
line = line + 1;
if (line == lines) {
if (filePointer == fileLength - 1) {
continue;
}
break;
}
}
sb.append((char) readByte);
}
String lastLine = sb.reverse().toString();
return lastLine;
} catch (java.io.FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (java.io.IOException e) {
e.printStackTrace();
return null;
} finally {
if (fileHandler != null) {
try {
fileHandler.close();
} catch (IOException e) {
}
}
}
}
Comments
Post a Comment