導航:首頁 > 編程語言 > 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