本帖最后由 menglin09 于 2010-08-16 20:43:40 编辑

解决方案 »

  1.   


    Java IO implementation of unix/linux “tail -f”
    http://stackoverflow.com/questions/557844/java-io-implementation-of-unix-linux-tail-f
      

  2.   

    “9 0 …………”开头 使用 String类的 startsWith 方法 而不是 contains
      

  3.   

    根据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();
    }
    }
    }
    }
      

  4.   

    1、根据\r\n一直找到最后几行
    2、判断开头是否是9 0
    3、存入数据结构进行操作。以前做过一个倒叙输出文件的,找不到了。
    楼主可以去www.java2000.net搜寻一下