@Test public void testTail() throws Exception { String[] lines = FileHelper.tail(lessLinesFilePath, maxLineCount); Assert.assertEquals(lines.length, maxLineCount - 1); lines = FileHelper.tail(exactLinesFilePath, maxLineCount); Assert.assertEquals(lines.length, maxLineCount); lines = FileHelper.tail(moreLinesFilePath, maxLineCount); Assert.assertEquals(lines.length, maxLineCount); }
public static String[] tail(String pathToFile, int lines) { lines++; RandomAccessFile fileHandler = null; try { fileHandler = new RandomAccessFile(pathToFile, "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++; if (line == lines) { if (filePointer == fileLength) { continue; } else { break; } } } else if (readByte == 0xD) { if (line == lines) { if (filePointer == fileLength - 1) { continue; } else { break; } } } sb.append((char) readByte); } String lastLine = sb.reverse().toString(); return lastLine.trim().split("\n"); } catch (IOException e) { LOGGER.warn(e.getMessage()); } finally { IOHelper.close(fileHandler); } return new String[0]; }
FileHelper { public static String[] tail(String pathToFile, int lines) { lines++; RandomAccessFile fileHandler = null; try { fileHandler = new RandomAccessFile(pathToFile, "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++; if (line == lines) { if (filePointer == fileLength) { continue; } else { break; } } } else if (readByte == 0xD) { if (line == lines) { if (filePointer == fileLength - 1) { continue; } else { break; } } } sb.append((char) readByte); } String lastLine = sb.reverse().toString(); return lastLine.trim().split("\n"); } catch (IOException e) { LOGGER.warn(e.getMessage()); } finally { IOHelper.close(fileHandler); } return new String[0]; } }
FileHelper { public static String[] tail(String pathToFile, int lines) { lines++; RandomAccessFile fileHandler = null; try { fileHandler = new RandomAccessFile(pathToFile, "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++; if (line == lines) { if (filePointer == fileLength) { continue; } else { break; } } } else if (readByte == 0xD) { if (line == lines) { if (filePointer == fileLength - 1) { continue; } else { break; } } } sb.append((char) readByte); } String lastLine = sb.reverse().toString(); return lastLine.trim().split("\n"); } catch (IOException e) { LOGGER.warn(e.getMessage()); } finally { IOHelper.close(fileHandler); } return new String[0]; } private  FileHelper(); }
FileHelper { public static String[] tail(String pathToFile, int lines) { lines++; RandomAccessFile fileHandler = null; try { fileHandler = new RandomAccessFile(pathToFile, "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++; if (line == lines) { if (filePointer == fileLength) { continue; } else { break; } } } else if (readByte == 0xD) { if (line == lines) { if (filePointer == fileLength - 1) { continue; } else { break; } } } sb.append((char) readByte); } String lastLine = sb.reverse().toString(); return lastLine.trim().split("\n"); } catch (IOException e) { LOGGER.warn(e.getMessage()); } finally { IOHelper.close(fileHandler); } return new String[0]; } private  FileHelper(); static String[] tail(String pathToFile, int lines); static File validateDir(File dir); }
FileHelper { public static String[] tail(String pathToFile, int lines) { lines++; RandomAccessFile fileHandler = null; try { fileHandler = new RandomAccessFile(pathToFile, "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++; if (line == lines) { if (filePointer == fileLength) { continue; } else { break; } } } else if (readByte == 0xD) { if (line == lines) { if (filePointer == fileLength - 1) { continue; } else { break; } } } sb.append((char) readByte); } String lastLine = sb.reverse().toString(); return lastLine.trim().split("\n"); } catch (IOException e) { LOGGER.warn(e.getMessage()); } finally { IOHelper.close(fileHandler); } return new String[0]; } private  FileHelper(); static String[] tail(String pathToFile, int lines); static File validateDir(File dir);  }