日期:2014-05-20  浏览次数:20680 次

java读取文本文件最后一行
一个不断增长的txt文本,
如何每隔5秒读取文本最后一行,
并显示出来.


对个编程新手,肠子都开写断了.
哪位大侠指点一下.
java

------解决方案--------------------
简单的定时任务可以用Timer

public static void main(String[] args) throws Exception {

Timer timer = new Timer();

timer.schedule(new TimerTask() {
public void run() {
try {
File file = new File("c:/data.txt");

BufferedReader br = new BufferedReader(new FileReader(file));

String buf = null;
String str = null;

if (file.length() > 1024 * 1024 * 1024) {
br.skip(file.length() / 3 * 2);// 当文件超过1M大小的时候,跳过文本长度的2/3,具体跳过多少视实际情况而定,文件越大可以跳过的部分越多
}

while ((buf = br.readLine()) != null) {
str = buf;
}

br.close();

System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}
}
}, 0, 5000);
}