导航:首页 > 编程语言 > java读取log

java读取log

发布时间:2023-06-14 00:09:53

java定时读取日志文件

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.io.ByteArrayInputStream;

class Reader implements Runnable {
Reader(String filename) {
this.filename = filename;
}

private String filename;
private long filelength = 0;
private int count=0;
@Override
public void run() {
while (true) {
try {
File f = new File(filename);
long nowlength = f.length();
long readlength = nowlength - filelength;
if (readlength == 0) {
Thread.sleep(1000);
continue;
}

RandomAccessFile rf = new RandomAccessFile(f, "r");
// 移动文件指针到上次读的最后
rf.seek(filelength);

filelength=nowlength;

byte[] b = new byte[(int) readlength];
rf.read(b, 0, b.length);
rf.close();

BufferedReader br=new BufferedReader(new InputStreamReader(new ByteArrayInputStream(b)));
String str=null;
count++;
System.out.println("第"+count+"次读到的内容:");
while((str=br.readLine())!=null){
System.out.println(str);
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

class Writer implements Runnable{
Writer(String filename) {
this.filename = filename;
}
private String filename;
private int count=0;

@Override
public void run() {
while (count++<100){
try {
PrintWriter pw=new PrintWriter(new FileWriter(filename,true));
pw.append(""+count).append("\t").append(""+System.currentTimeMillis()).append("写入的内容").append("\r\n");
pw.close();
Thread.sleep(100);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

}

public class ReadFileFromTrail {
public static void main(String[] args) {
Reader reader=new Reader("d:\\test.log");
Writer writer=new Writer("d:\\test.log");
new Thread(reader).start();
new Thread(writer).start();
}

}

② java如何从后缀为log的文件中提取数据

public static void main(String[] args) {
String fromFile = System.getProperty("user.dir") + File.separatorChar + "user.info";
String toFile = System.getProperty("user.dir") + File.separatorChar + "user1.info";
Reader r = null;
Writer w = null;
try {
r = new FileReader(fromFile);
w = new FileWriter(toFile);
} catch (IOException e) {
System.out.println(e.getMessage());
System.exit(0);
}
BufferedReader br = new BufferedReader(r);
BufferedWriter bw = new BufferedWriter(w);
// int s = -1;
String line = null;
try {
// while ((s = br.read()) != -1) {
// System.out.print((char)s);
// bw.write(s);
// }
while ((line = br.readLine()) != null) {
System.out.println(line);
bw.write(line);
bw.newLine();
// bw.write("\t\n");
}
bw.flush();
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
try {
bw.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
try {
br.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}

看不看得懂就是你的事了,不做解答。

阅读全文

与java读取log相关的资料

热点内容
如何看漫威漫画app 浏览:789
安卓手机如何按拼音排布app 浏览:721
java中exceptionin 浏览:882
java131 浏览:868
学英语不登录的app哪个最好 浏览:299
安卓的后台运行怎么设置 浏览:135
如何撰写论文摘要以及编译sci 浏览:416
安卓如何使用推特贴吧 浏览:429
怎样避免程序员入狱 浏览:856
苹果方块消除安卓叫什么 浏览:535
安卓世界征服者2怎么联机 浏览:297
国企招的程序员 浏览:969
哪个app可以看watch 浏览:518
dns备用什么服务器 浏览:1002
中达优控触摸屏编译失败 浏览:80
上海科纳压缩机 浏览:680
python工时系统 浏览:551
查好友ip命令 浏览:118
通达信python量化交易 浏览:506
cnc编程工程师自我评价 浏览:133