導航:首頁 > 編程語言 > 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如何導流水 瀏覽: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
籌碼尺指標公式源碼 瀏覽:534
如何在瀏覽器訪問阿里雲伺服器 瀏覽:538
火的企業文件加密 瀏覽:463
支付寶app如何重新注冊 瀏覽:520
現在的伺服器版本是什麼 瀏覽:192
電阻在單片機 瀏覽:502