public static String tail2 ( File file , int lines ) { lines ++ ; // Read # lines inclusive 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 - 1 ) { continue ; } else { 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 ) { /* ignore */ } } }
Read the last few lines of a file