我的需求是这样的,读取一个日志文件读到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();
}
}
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();
}
}
放在run方法里吧
还有一个你要注意的地方是里面的参数是字节数,而不是行数,所以用100肯定小了。
还有,判断每次是否读满100行,要以是否除尽100来判断,而不能用==100来判断。
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();
}
}
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();
}
}