飞云小侠的个人博客

欢迎你到这里来

« 文件排序:按时间,大小等 | Main | 配置Subversion必须填写注释的脚本Hook »

显示文件的倒数XX行
2006/05/12,17:43
读取日志时经常想到这个方法,但是苦于Java的文件操作函数太少.

想到使用随机文件类读取最后一行,然后回退读取,判断回车,直到回车数到达你要求的函数,然后就ok了

使用google搜索一下,竟然找到一个现成了,太爽了

http://crawler.archive.org/xref-test/org/archive/crawler/util/LogReader.html

不过这个东西实在非我能承受,只好copy出来,注明出处了

相关的片断:

  1. /**
  2. * Implementation of a unix-like 'tail' command
  3. *
  4. * @param aFileName a file name String
  5. * @return An array of two strings is returned. At index 0 the String
  6. * representation of at most 10 last lines is located.
  7. * At index 1 there is an informational string about how large a
  8. * segment of the file is being returned.
  9. * Null is returned if errors occur (file not found or io exception)
  10. */
  11. public static String[] tail(String aFileName) {
  12. return tail(aFileName, 10);
  13. }
  14.  
  15. /**
  16. * Implementation of a unix-like 'tail -n' command
  17. *
  18. * @param aFileName a file name String
  19. * @param n int number of lines to be returned
  20. * @return An array of two strings is returned. At index 0 the String
  21. * representation of at most n last lines is located.
  22. * At index 1 there is an informational string about how large a
  23. * segment of the file is being returned.
  24. * Null is returned if errors occur (file not found or io exception)
  25. */
  26. public static String[] tail(String aFileName, int n) {
  27. try {
  28. return tail(new RandomAccessFile(new File(aFileName),"r"),n);
  29. } catch (FileNotFoundException e) {
  30. e.printStackTrace();
  31. return null;
  32. }
  33. }
  34.  
  35. /**
  36. * Implementation of a unix-like 'tail -n' command
  37. *
  38. * @param raf a RandomAccessFile to tail
  39. * @param n int number of lines to be returned
  40. * @return An array of two strings is returned. At index 0 the String
  41. * representation of at most n last lines is located.
  42. * At index 1 there is an informational string about how large a
  43. * segment of the file is being returned.
  44. * Null is returned if errors occur (file not found or io exception)
  45. */
  46. public static String[] tail(RandomAccessFile raf, int n) {
  47. int BUFFERSIZE = 1024;
  48. long pos;
  49. long endPos;
  50. long lastPos;
  51. int numOfLines = 0;
  52. String info=null;
  53. byte[] buffer = new byte[BUFFERSIZE];
  54. StringBuffer sb = new StringBuffer();
  55. try {
  56. endPos = raf.length();
  57. lastPos = endPos;
  58.  
  59. // Check for non-empty file
  60. // Check for newline at EOF
  61. if (endPos > 0) {
  62. byte[] oneByte = new byte[1];
  63. raf.seek(endPos - 1);
  64. raf.read(oneByte);
  65. if ((char) oneByte[0] != ' ') {
  66. numOfLines++;
  67. }
  68. }
  69.  
  70. do {
  71. // seek back BUFFERSIZE bytes
  72. // if length of the file if less then BUFFERSIZE start from BOF
  73. pos = 0;
  74. if ((lastPos - BUFFERSIZE) > 0) {
  75. pos = lastPos - BUFFERSIZE;
  76. }
  77. raf.seek(pos);
  78. // If less then BUFFERSIZE avaliable read the remaining bytes
  79. if ((lastPos - pos) < BUFFERSIZE) {
  80. int remainer = (int) (lastPos - pos);
  81. buffer = new byte[remainer];
  82. }
  83. raf.readFully(buffer);
  84. // in the buffer seek back for newlines
  85. for (int i = buffer.length - 1; i >= 0; i--) {
  86. if ((char) buffer[i] == ' ') {
  87. numOfLines++;
  88. // break if we have last n lines
  89. if (numOfLines > n) {
  90. pos += (i + 1);
  91. break;
  92. }
  93. }
  94. }
  95. // reset last postion
  96. lastPos = pos;
  97. } while ((numOfLines <= n) && (pos != 0));
  98.  
  99. // print last n line starting from last postion
  100. for (pos = lastPos; pos < endPos; pos += buffer.length) {
  101. raf.seek(pos);
  102. if ((endPos - pos) < BUFFERSIZE) {
  103. int remainer = (int) (endPos - pos);
  104. buffer = new byte[remainer];
  105. }
  106. raf.readFully(buffer);
  107. sb.append(new String(buffer));
  108. }
  109.  
  110. info = buildDisplayingHeader(sb.length(), raf.length());
  111. } catch (FileNotFoundException e) {
  112. sb = null;
  113. } catch (IOException e) {
  114. e.printStackTrace();
  115. sb = null;
  116. } finally {
  117. try {
  118. if (raf != null) {
  119. raf.close();
  120. }
  121. } catch (IOException e) {
  122. e.printStackTrace();
  123. }
  124. }
  125. if(sb==null){
  126. return null;
  127. }
  128. String[] tmp = {sb.toString(),info};
  129. return tmp;
  130. }

 

最新回复

 
Support by JavaScud