java捕獲異常信息存入txt文件示例
捕獲程序中出現(xiàn)的異常 可用于后期維護(hù)的必要性!做簡單的測試 !
package helpEntity;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Log {
private File file = null;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public void saveLog(Exception e, String youName) {
try {
String nowPath = null;
nowPath = System.getProperty("user.dir");
String tempPath = null;
this.file = new File(nowPath);
tempPath = this.file.getParent();
if (tempPath == null) {
this.file = new File(nowPath);
}
this.file = new File(tempPath + "" + File.separator + "log.txt");
PrintWriter writer = null;
FileWriter fileWrite = new FileWriter(file, true);
writer = new PrintWriter(fileWrite);
writer.append(System.getProperty("line.separator")
+ new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss")
.format(new Date()) + "__" + youName);
writer.append(System.getProperty("line.separator"));
writer.append(" *************************" + e.toString()
+ "*************************");
writer.append(System.getProperty("line.separator"));
e.printStackTrace(writer);
writer.flush();
writer.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
相關(guān)文章
java使用RSA加密方式實現(xiàn)數(shù)據(jù)加密解密的代碼
這篇文章給大家分享java使用RSA加密方式實現(xiàn)數(shù)據(jù)加密解密,通過實例代碼文字相結(jié)合給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友參考下2019-11-11
Java線程安全解決方案(synchronized,ReentrantLock,Atomic)
這篇文章主要介紹了Java線程安全解決方案(synchronized,ReentrantLock,Atomic),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
使用Spring-Retry解決Spring Boot應(yīng)用程序中的重試問題
重試的使用場景比較多,比如調(diào)用遠(yuǎn)程服務(wù)時,由于網(wǎng)絡(luò)或者服務(wù)端響應(yīng)慢導(dǎo)致調(diào)用超時,此時可以多重試幾次。用定時任務(wù)也可以實現(xiàn)重試的效果,但比較麻煩,用Spring Retry的話一個注解搞定所有,感興趣的可以了解一下2023-04-04
lambda表達(dá)式與傳統(tǒng)接口函數(shù)實現(xiàn)方式對比詳解
這篇文章主要為大家介紹了lambda表達(dá)式與傳統(tǒng)接口函數(shù)實現(xiàn)方式對比詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家度偶多進(jìn)步早日升職加薪2022-03-03
詳解自動注冊Gateway網(wǎng)關(guān)路由配置
這篇文章主要為大家介紹了自動注冊Gateway網(wǎng)關(guān)路由配置的方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

