p48376382 发表于 2013-1-27 05:22:47

读取文件的最后一行

public class readFile {public static void main(String[] args) {try {RandomAccessFile rf = new RandomAccessFile("c:\\Analysen.BAK", "r");long len = rf.length();long start = rf.getFilePointer();long nextend = start + len - 1;String line = null;rf.seek(nextend);int c = -1;while (nextend > start) {c = rf.read();if (c == '\n' || c == '\r') {line = rf.readLine();if (line == null || line.trim().equals("")) {nextend--;rf.seek(nextend);continue;}break;}nextend--;rf.seek(nextend);if (nextend == 0) {// 当文件指针退至文件开始处,输出第一行line = rf.readLine();}}// 处理函数System.out.print(line);} catch (Exception e) {System.out.println("Error handling a client: " + e);}}} 
页: [1]
查看完整版本: 读取文件的最后一行