Java--IO--文件--常用操作

电玩女神 2023-01-13 04:09 273阅读 0赞

原文网址:Java—IO—文件—常用操作_IT利刃出鞘的博客-CSDN博客

读取文件最后n行

方案1:指定编码格式(推荐)

其他网址

java读取文件后n行_zzm628的专栏-CSDN博客

代码

  1. public class ReadFile {
  2. public static void main(String[] args) {
  3. //调用读取方法,定义文件以及读取行数
  4. List<String> list = readLastNLine(new File("D:/1.txt"), 5L);
  5. if (list != null) {
  6. for (String str : list) {
  7. System.out.println(str + "<br>");
  8. }
  9. }
  10. }
  11. /**
  12. * 读取文件最后N行
  13. * <p>
  14. * 根据换行符判断当前的行数,
  15. * 使用统计来判断当前读取第N行
  16. * <p>
  17. * PS:输出的List是倒叙,需要对List反转输出
  18. *
  19. * @param file 待文件
  20. * @param numRead 读取的行数
  21. * @return List<String>
  22. */
  23. public static List<String> readLastNLine(File file, long numRead) {
  24. // 定义结果集
  25. List<String> result = new ArrayList<String>();
  26. //行数统计
  27. long count = 0;
  28. // 排除不可读状态
  29. if (!file.exists() || file.isDirectory() || !file.canRead()) {
  30. return null;
  31. }
  32. // 使用随机读取
  33. RandomAccessFile fileRead = null;
  34. try {
  35. //使用读模式
  36. fileRead = new RandomAccessFile(file, "r");
  37. //读取文件长度
  38. long length = fileRead.length();
  39. //如果是0,代表是空文件,直接返回空结果
  40. if (length == 0L) {
  41. return result;
  42. } else {
  43. //初始化游标
  44. long pos = length - 1;
  45. while (pos > 0) {
  46. pos--;
  47. //开始读取
  48. fileRead.seek(pos);
  49. //如果读取到\n代表是读取到一行
  50. if (fileRead.readByte() == '\n') {
  51. //使用readLine获取当前行
  52. String line = new String(fileRead.readLine().getBytes("ISO-8859-1"), "utf-8");
  53. //保存结果
  54. result.add(line);
  55. //打印当前行
  56. //System.out.println(line);
  57. //行数统计,如果到达了numRead指定的行数,就跳出循环
  58. count++;
  59. if (count == numRead) {
  60. break;
  61. }
  62. }
  63. }
  64. if (pos == 0) {
  65. fileRead.seek(0);
  66. result.add(fileRead.readLine());
  67. }
  68. }
  69. } catch (IOException e) {
  70. e.printStackTrace();
  71. } finally {
  72. if (fileRead != null) {
  73. try {
  74. //关闭资源
  75. fileRead.close();
  76. } catch (Exception e) {
  77. }
  78. }
  79. }
  80. Collections.reverse(result);
  81. return result;
  82. }
  83. }

方案2:使用系统编码格式(不推荐)

其他网址

Java读取文件最后n行_益达哥哥的博客-CSDN博客_java读取文件最后n行

代码

  1. public static String readLastRows(String filePath, int rows) throws IOException {
  2. Charset charset = Charset.defaultCharset();
  3. try (RandomAccessFile rf = new RandomAccessFile(filePath, "r")) {
  4. // 每次读取的字节数要和系统换行符大小一致
  5. byte[] c = new byte[1];
  6. // 在获取到指定行数和读完文档之前,从文档末尾向前移动指针,遍历文档每一个字节
  7. for (long pointer = rf.length(), lineSeparatorNum = 0; pointer >= 0 && lineSeparatorNum < rows;) {
  8. // 移动指针
  9. rf.seek(pointer--);
  10. // 读取数据
  11. int readLength = rf.read(c);
  12. if (readLength != -1 && c[0] == 10) {
  13. lineSeparatorNum++;
  14. }
  15. // 扫描完依然没有找到足够的行数,将指针归0
  16. if (pointer == -1 && lineSeparatorNum < rows) {
  17. rf.seek(0);
  18. }
  19. }
  20. byte[] tempbytes = new byte[(int) (rf.length() - rf.getFilePointer())];
  21. rf.readFully(tempbytes);
  22. return new String(tempbytes, charset);
  23. }
  24. }

发表评论

表情:
评论列表 (有 0 条评论,273人围观)

还没有评论,来说两句吧...

相关阅读

    相关 javaIO操作(上)

    IO原理 • 程序运行在内存和CPU所构成的资源里,可是有很多东西需要 程序来操作,它们都不在内存里,比如键盘、硬盘、打印机、 网络等。 • 在内存中的程序,需

    相关 ant 文件操作

    4.5 Copy Task:对文件和目录进行复制 Copy 任务把一个或多个文件复制到指定的目录下。但要注意的是,如果目标目录下具有同名的文件,那么只有当源文件相对于目标