« 文件排序:按时间,大小等 | Main | 配置Subversion必须填写注释的脚本Hook »
想到使用随机文件类读取最后一行,然后回退读取,判断回车,直到回车数到达你要求的函数,然后就ok了
使用google搜索一下,竟然找到一个现成了,太爽了
http://crawler.archive.org/xref-test/org/archive/crawler/util/LogReader.html
不过这个东西实在非我能承受,只好copy出来,注明出处了
相关的片断:
/** * Implementation of a unix-like 'tail' command * * @param aFileName a file name String * @return An array of two strings is returned. At index 0 the String * representation of at most 10 last lines is located. * At index 1 there is an informational string about how large a * segment of the file is being returned. * Null is returned if errors occur (file not found or io exception) */ public static String[] tail(String aFileName) { return tail(aFileName, 10); } /** * Implementation of a unix-like 'tail -n' command * * @param aFileName a file name String * @param n int number of lines to be returned * @return An array of two strings is returned. At index 0 the String * representation of at most n last lines is located. * At index 1 there is an informational string about how large a * segment of the file is being returned. * Null is returned if errors occur (file not found or io exception) */ public static String[] tail(String aFileName, int n) { try { return tail(new RandomAccessFile(new File(aFileName),"r"),n); } catch (FileNotFoundException e) { e.printStackTrace(); return null; } } /** * Implementation of a unix-like 'tail -n' command * * @param raf a RandomAccessFile to tail * @param n int number of lines to be returned * @return An array of two strings is returned. At index 0 the String * representation of at most n last lines is located. * At index 1 there is an informational string about how large a * segment of the file is being returned. * Null is returned if errors occur (file not found or io exception) */ public static String[] tail(RandomAccessFile raf, int n) { int BUFFERSIZE = 1024; long pos; long endPos; long lastPos; int numOfLines = 0; String info=null; byte[] buffer = new byte[BUFFERSIZE]; StringBuffer sb = new StringBuffer(); try { endPos = raf.length(); lastPos = endPos; // Check for non-empty file // Check for newline at EOF if (endPos > 0) { byte[] oneByte = new byte[1]; raf.seek(endPos - 1); raf.read(oneByte); if ((char) oneByte[0] != ' ') { numOfLines++; } } do { // seek back BUFFERSIZE bytes // if length of the file if less then BUFFERSIZE start from BOF pos = 0; if ((lastPos - BUFFERSIZE) > 0) { pos = lastPos - BUFFERSIZE; } raf.seek(pos); // If less then BUFFERSIZE avaliable read the remaining bytes if ((lastPos - pos) < BUFFERSIZE) { int remainer = (int) (lastPos - pos); buffer = new byte[remainer]; } raf.readFully(buffer); // in the buffer seek back for newlines for (int i = buffer.length - 1; i >= 0; i--) { if ((char) buffer[i] == ' ') { numOfLines++; // break if we have last n lines if (numOfLines > n) { pos += (i + 1); break; } } } // reset last postion lastPos = pos; } while ((numOfLines <= n) && (pos != 0)); // print last n line starting from last postion for (pos = lastPos; pos < endPos; pos += buffer.length) { raf.seek(pos); if ((endPos - pos) < BUFFERSIZE) { int remainer = (int) (endPos - pos); buffer = new byte[remainer]; } raf.readFully(buffer); sb.append(new String(buffer)); } info = buildDisplayingHeader(sb.length(), raf.length()); } catch (FileNotFoundException e) { sb = null; } catch (IOException e) { e.printStackTrace(); sb = null; } finally { try { if (raf != null) { raf.close(); } } catch (IOException e) { e.printStackTrace(); } } if(sb==null){ return null; } String[] tmp = {sb.toString(),info}; return tmp; }
| « | 七月 2008 | » | ||||
|---|---|---|---|---|---|---|
| 一 | 二 | 三 | 四 | 五 | 六 | 日 |
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 | |||