我的需求是这样的,读取一个日志文件读到100行的时候进行暂停,10秒后继续往下读...我现在的代码是一次性读完,然后10秒后再读一遍,请高手们看看package Test;import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;public class test2 implements Runnable{ public void run() {
while(true){
try {
Thread.sleep(10*1000);
try {
test();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
 
}
}
 public static void readLines(LineNumberReader lnr) throws IOException{   
        String line=null;   
        int count=0;   
        while((line=lnr.readLine())!=null){   
            System.out.println(lnr.getLineNumber()+"\t"+line);   
            if(lnr.getLineNumber()==100){//当运行到第5行   
                //在当前位置打上一个书签   
                lnr.(100); 
                System.out.println("-------------------------------");
                break;
            }   
        }   
    }
 public static void test()throws IOException{
 LineNumberReader lnr=new LineNumberReader(new FileReader("D:/proxy/MilleSystem/proxy.log.20101119"));   
        
        lnr.setLineNumber(1);//只影响行号的展现形式,而不具有其他的实质意义   
        readLines(lnr);   
        //与()方法配合使用,翻到最近一次打开的书签   
           
        lnr.reset();   
        readLines(lnr);   
        lnr.close(); 
 }
 public static void main(String []aaa){
 test2 t=new test2();
 t.run();
 }
}

解决方案 »

  1.   

    这个打开文件的动作,不要放在test方法中
    放在run方法里吧
      

  2.   

    顺便问一句,这Log文件老变么?
      

  3.   

    代码不是太多,直接放到RUN中去执行。
      

  4.   

    不知道你搞出来没有。1楼我指出的那点,原因是你在第一个流上做的,新建的第二个流上肯定无法reset,所以文件要在run方法中开,并一直保持打开,除非程序结束。
    还有一个你要注意的地方是里面的参数是字节数,而不是行数,所以用100肯定小了。
    还有,判断每次是否读满100行,要以是否除尽100来判断,而不能用==100来判断。
      

  5.   

    我写了下,LZ要打好基础啊import java.io.FileReader;
    import java.io.BufferedReader;
    import java.io.IOException;class PausePrinter implements Runnable
    {
    String fileStr;
    public PausePrinter(String fileStr)
    {
    this.fileStr=fileStr;
    }
    public void run()
    {
    int lno=0;
    String line="";
    try
    {
    BufferedReader br = new BufferedReader(new FileReader(fileStr));
    while((line=br.readLine())!=null)
    {
    System.out.println(++lno+"\t"+line);
    if(lno==100)
    {
    lno=0;
    Thread.sleep(10*1000);
    }
    }
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }
    catch (InterruptedException e)
    {
    e.printStackTrace();
    }
    } public static void main(String[] args) 
    {
    PausePrinter pp=new PausePrinter(args[0]);
    Thread ppt = new Thread(pp);
    ppt.start();
    }
    }
      

  6.   

    就你的代码改的public class ReadingLogFileByMark implements Runnable {
    public void run() {
    LineNumberReader lnr;
    try {
    lnr = new LineNumberReader(new FileReader(
    "D:/proxy/MilleSystem/proxy.log.20101119"));
    lnr.(100*100);
    while (true) {
    try {
    Thread.sleep(10 * 1000);
    try {
    readLines(lnr);
    } catch (IOException e) {
    e.printStackTrace();
    break;
    }
    } catch (InterruptedException e1) {
    e1.printStackTrace();
    } }
    lnr.close();
    } catch (Exception e2) {
    e2.printStackTrace();
    }
    } public static void readLines(LineNumberReader lnr) throws IOException {
    String line = null;
    int count = 0;
    lnr.reset();
    while ((line = lnr.readLine()) != null) {
    count++;
    System.out.println(lnr.getLineNumber() + "\t" + line);
    if (count >= 100) {
    System.out.println("------100 lines reached.-------------------------");
    break;
    }
    }
    // 在当前位置打上一个书签
    lnr.(100*100);
    } public static void main(String[] aaa) {
    ReadingLogFileByMark t = new ReadingLogFileByMark();
    t.run();

    }
    }
      

  7.   

    LZ好牛B啊,竟然写了这么多的代码