返回文本文件最后一行java代码
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| public static String readLastLine(File file, String charset) throws IOException { if (!file.exists() || file.isDirectory() || !file.canRead()) { return null; } RandomAccessFile raf = null; try { raf = new RandomAccessFile(file, "r"); long len = raf.length(); if (len == 0L) { return ""; } else { long pos = len - 1; while (pos > 0) { pos--; raf.seek(pos); if (raf.readByte() == '\n') { break; } } if (pos == 0) { raf.seek(0); } byte[] bytes = new byte[(int) (len - pos)]; raf.read(bytes); if (charset == null) { return new String(bytes); } else { return new String(bytes, charset); } } } catch (FileNotFoundException e) { } finally { if (raf != null) { try { raf.close(); } catch (Exception e2) { } } } return null; }
|
参考:https://blog.csdn.net/q_linchao/article/details/79630906
读取本文文件最后一行,并记下最后一行的位置
如果文本文件最后一行也有特殊的意义,需要动态的更新最后一行,单单读取最后一行是远远不够的,所以我在上面代码的基础上进一步封装成一个工具类,把文本文件的最后一行,以及最后一行的位置。保存在内部成员属性中,源代码如下:
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile;
public class LastLineInFile { private long lastlinestart; private String lastLine; public LastLineInFile() { this.lastlinestart=0; this.lastLine=null; } public long getLastlinestart() { return lastlinestart; } public String getLastLine() { return lastLine; } private void setLastlinestart(long pos) { this.lastlinestart = pos; } private void setLastLine(String lastLine) { this.lastLine = lastLine; } public void readLastLine(File file, String charset) throws IOException { RandomAccessFile raf = null; try { raf = new RandomAccessFile(file, "r"); long len = raf.length(); if (len == 0L) { setLastLine(""); } else { long pos = len - 1; while (pos > 0) { pos--; raf.seek(pos); if (raf.readByte() == '\n') { break; } } if (pos == 0) { raf.seek(0); } setLastlinestart(pos); byte[] bytes = new byte[(int) (len - pos)]; raf.read(bytes); if (charset == null) { setLastLine(new String(bytes)); } else { setLastLine(new String(bytes,charset)); } } } catch (FileNotFoundException e) { } finally { if (raf != null) { try { raf.close(); } catch (Exception e2) { } } } } }
|
这样我记下了最后一行的位置之后,就可以动态的更新最后一行了,使用代码片段如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| LastLineInFile lastLineInFile=new LastLineInFile(); File localFile=new File(localPath);
lastLineInFile.readLastLine(localFile, "utf-8");
String lastLine=lastLineInFile.getLastLine();
RandomAccessFile rf=new RandomAccessFile(localFile, "rwd");
long filelastpos=lastLineInFile.getLastlinestart(); System.out.println("最后一行位置:"+filelastpos);
rf.setLength(filelastpos);
rf.seek(filelastpos);
rf.write(hyperLinks.getBytes("utf-8"));
rf.close();
|