在java文件操作的時候,思考將日誌信息存到txt中,現在很多項目都是通過log4j來做的,同樣也會用到將日誌存到txt中.
package FileOperation;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
/**
* Created by CXC on 2017/6/5.
*/
public class Log_Exception {
/**
* @將錯誤信息輸入到txt中
* @param path
* @throws IOException
*/
public void writeEror_to_txt(String path,String content) throws IOException{
File F=new File(path);
//如果文件不存在,就動態創建文件
if(!F.exists()){
F.createNewFile();
}
FileWriter fw=null;
String writeDate="時間:"+this.get_nowDate()+"---"+"error:"+content;
try {
//設置爲:True,表示寫入的時候追加數據
fw=new FileWriter(F, true);
//回車並換行
fw.write(writeDate+"\r\n");
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fw!=null){
fw.close();
}
}
}
/**
* @獲取系統當前時間
* @return
*/
public String get_nowDate(){
Calendar D=Calendar.getInstance();
int year=0;
int moth=0;
int day=0;
year=D.get(Calendar.YEAR);
moth=D.get(Calendar.MONTH)+1;
day=D.get(Calendar.DAY_OF_MONTH);
String now_date=String.valueOf(year)+"-"+String.valueOf(moth)+"-"+String.valueOf(day);
return now_date;
}
//測試方法
public static void main(String[] args) throws IOException {
String path="E:/filezl.txt";
String content = null;
try{
// String i="";
// if(i==null){
// System.out.println("111");
// content="hello 你好!天天向上!";
// }
// String[] strings={"1","2","3"};
List<String> list=new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
for(String i:list){
System.out.println(i);
}
String j=list.get(3);
}catch (Exception e){
content=e.getClass().getName()+" error Info "+e.getMessage();
// content=e.getMessage();
}
Log_Exception le=new Log_Exception();
le.writeEror_to_txt(path, content);
}
}
效果圖: