导航:首页 > 编程语言 > 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相关的资料

热点内容
unix查看端口命令 浏览:21
程序员团建活动有哪些 浏览:67
libzip压缩解压 浏览:943
广州银行app如何导流水 浏览:385
什么是写命令 浏览:687
程序员眼中的道德 浏览:505
文件解压后乱码是什么原因 浏览:730
php路径转换 浏览:188
php中flag 浏览:182
当程序员要做什么准备 浏览:284
pinctrllinux 浏览:901
lzw压缩工具 浏览:864
luae加密插件破解版 浏览:141
Python求导数和程序 浏览:223
太原java程序员 浏览:361
picfile是什么文件夹 浏览:292
12乘12国外的算法 浏览:182
筹码尺指标公式源码 浏览:535
如何在浏览器访问阿里云服务器 浏览:538
火的企业文件加密 浏览:463