我想在java代碼中創建一個日誌。而且,整個過程需要數小時才能完成。但是,我的代碼在執行結束時將所有日誌都記錄下來,因爲緩衝區在最後被關閉。如何在日誌中添加下面的代碼中的一些字符串
是否有任何方法,以便我可以附加在日誌文件中?
請參閱我的代碼,並提出一些必要的更改。 在此先感謝。
我的代碼,
public class test {
public static void func(String url1, String url2, BufferedWriter bw) throws InterruptedException, IOException {
long loading_time = 10878;
long parsing_time = 120;
long processing_time = 329;
Thread.sleep(100000000); // Here is some process that performs recursively
bw.write(url1 + "\n" + url2 + "\n"+ ((double)loading_time)/((double)1000)+ " seconds " + ((double)parsing_time)/((double)1000) + " seconds " + ((double)processing_time)/((double)1000) + " seconds\n\n\n\n");
}
public static void main(String[] args) throws IOException, InterruptedException{
try {
FileWriter fw = new FileWriter("C:\\Users\\jhamb\\Desktop\\log.txt");
BufferedWriter bw = new BufferedWriter(fw);
for(int i = 0; i < 5; i++) { func("hitesh", "jhamb", bw); }
bw.close();
} catch (Exception e) {
System.out.println("Sorry some problem");
}
}
}
使用任何日誌框架 – rajesh 2013-04-10 12:14:37
@rajesh請參閱我的上述代碼,並請建議我如何更改此? – devsda 2013-04-10 12:15:26