如何从文本末尾读正确的一行数据 本帖最后由 menglin09 于 2010-08-16 20:43:40 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Java IO implementation of unix/linux “tail -f”http://stackoverflow.com/questions/557844/java-io-implementation-of-unix-linux-tail-f “9 0 …………”开头 使用 String类的 startsWith 方法 而不是 contains 根据ls的url编写如下code。线程1写入,线程2等待10秒,从最后一行读取线程1的文件。import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.util.Date;public class Test { public static void main(String[] args) { WorkThread2 thread2 = new WorkThread2(false); WorkThread1 thread1 = new WorkThread1(thread2); new Thread(thread1).start(); new Thread(thread2).start(); } private static class WorkThread1 implements Runnable{ int counter = 0; private WorkThread2 thread2; public WorkThread1(WorkThread2 thread2) { this.thread2 = thread2; } public void run() { System.out.println("in thread1.."); File file = new File("tmp.txt"); int count = 20; try { FileWriter fw = new FileWriter(file); while(count > 0) { fw.write(counter + " " + new Date() + "\n"); counter++; fw.flush(); Thread.sleep(1000); count--; } fw.close(); thread2.setFinish(true); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } } private static class WorkThread2 implements Runnable{ private boolean isFinish; public WorkThread2(boolean isFinish) { this.isFinish = isFinish; } public void setFinish(boolean isFinish) { this.isFinish = isFinish; } public void run() { //wait 10 seconds for workthread1 write records. try { Thread.sleep(10000); } catch (InterruptedException e1) { e1.printStackTrace(); } System.out.println("in thread2.."); File file = new File("tmp.txt"); while(!file.exists()) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } try { InputStream is = new FileInputStream(file); long skip = is.available(); is.close(); BufferedReader br = new BufferedReader(new FileReader(file)); br.skip(skip); String line = null; while(!isFinish) { line = br.readLine(); if(line == null) { Thread.sleep(1000); }else { //deal with your data. System.out.println(line); } } br.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }} 1、根据\r\n一直找到最后几行2、判断开头是否是9 03、存入数据结构进行操作。以前做过一个倒叙输出文件的,找不到了。楼主可以去www.java2000.net搜寻一下 冒泡错误 java学习书籍 为什么打开第二个图像后,移动滚动条又回到了原来的图像 JAVA序列化 中出现问题 一个java的问题,谁知道这种语法是什么? 如何动态地从jpanel里删除组件,然后添加一个 控制系统关闭退出 阅读了JDK1.5的API的Javadoc,觉得Integer类中有几个方法不懂,谁来解释下 怎么检查文件"c:\temp.txt是否存在,如果不存在怎么创建? Jbuilder 4中游标的问题 求一些Netty3程序源代码 关于 GetPropertyAction 的问题
Java IO implementation of unix/linux “tail -f”
http://stackoverflow.com/questions/557844/java-io-implementation-of-unix-linux-tail-f
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
public class Test { public static void main(String[] args) {
WorkThread2 thread2 = new WorkThread2(false);
WorkThread1 thread1 = new WorkThread1(thread2);
new Thread(thread1).start();
new Thread(thread2).start();
}
private static class WorkThread1 implements Runnable{ int counter = 0;
private WorkThread2 thread2;
public WorkThread1(WorkThread2 thread2) {
this.thread2 = thread2;
}
public void run() {
System.out.println("in thread1..");
File file = new File("tmp.txt");
int count = 20;
try {
FileWriter fw = new FileWriter(file);
while(count > 0) {
fw.write(counter + " " + new Date() + "\n");
counter++;
fw.flush();
Thread.sleep(1000);
count--;
}
fw.close();
thread2.setFinish(true);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static class WorkThread2 implements Runnable{ private boolean isFinish;
public WorkThread2(boolean isFinish) {
this.isFinish = isFinish;
}
public void setFinish(boolean isFinish) {
this.isFinish = isFinish;
}
public void run() {
//wait 10 seconds for workthread1 write records.
try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println("in thread2..");
File file = new File("tmp.txt");
while(!file.exists()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
InputStream is = new FileInputStream(file);
long skip = is.available();
is.close();
BufferedReader br = new BufferedReader(new FileReader(file));
br.skip(skip);
String line = null;
while(!isFinish) {
line = br.readLine();
if(line == null) {
Thread.sleep(1000);
}else {
//deal with your data.
System.out.println(line);
}
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
2、判断开头是否是9 0
3、存入数据结构进行操作。以前做过一个倒叙输出文件的,找不到了。
楼主可以去www.java2000.net搜寻一下