1. 程式人生 > >將需要書寫的內容,以追加的方式寫到檔案中

將需要書寫的內容,以追加的方式寫到檔案中

public void writeScriptLog(String writeInfo,Boolean isError) {
File folder =new File("目錄");
try {
if (!folder.exists()) {
folder.mkdirs();
}
File logFile =new File("目錄/檔案");
if (!logFile.exists()) {
logFile.createNewFile();
}
FileOutputStream fos = null;
OutputStreamWriter osw =null;
fos = new FileOutputStream(logFile.getPath(), true);  
osw = new OutputStreamWriter(fos);
String data=new SimpleDateFormat("MM/dd HH:mm:ss").format(new Date());
if (!isError) {
osw.write(data+" Info:"+writeInfo+ System.getProperty("line.separator"));
}else {
osw.write(data+" Error:"+writeInfo+ System.getProperty("line.separator"));
}
osw.close();
} catch (Exception e) {
e.printStackTrace();
}
}