Tail function in JAVA. Get the last lines of a given file. Obtaining the last lines of a large file 'efficiently': /** * 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; }
Posts
Showing posts from May, 2015