導航:首頁 > 編程語言 > 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相關的資料

熱點內容
程序員放棄後會怎樣 瀏覽:182
河北模具編程 瀏覽:187
adb查找命令 瀏覽:321
安卓手機視頻文件夾怎麼打開 瀏覽:309
平板加密手機後怎麼關閉 瀏覽:567
流媒體伺服器應該注意什麼 瀏覽:536
d8命令編譯 瀏覽:964
壓縮包解壓需要多少空間 瀏覽:145
如何查找app屬性 瀏覽:388
android人臉識別技術 瀏覽:322
pc104編程 瀏覽:335
二維碼反編譯破解推廣 瀏覽:682
修改伺服器的mac地址 瀏覽:528
好玩的編程軟體 瀏覽:899
編程語言創始人有錢嗎 瀏覽:804
短視頻app怎麼獲客 瀏覽:15
查看雲伺服器的應用 瀏覽:436
javadump工具 瀏覽:565
程序員16g 瀏覽:445
程序員沒有辦法成為top怎麼辦 瀏覽:218