首页 > 技术文章 > java 写入文件

caer 2016-11-10 15:30 原文

public void writeSqlLogs() throws IOException {
//创建日志

String update_sql_file_path="C:\\Users\\Administrator\\Desktop\\update_sql.txt";

String rollbakc_sql_file_path="C:\\Users\\Administrator\\Desktop\\rollback_sql.txt";
FileWriter update_sql_file = null; //log文件1
FileWriter rollbakc_sql_file = null;//log文件2
update_sql_file = new FileWriter(update_sql_file_path);//不保留上次记录,想保留上次记录参数设置为(update_sql_file_path,true)
rollbakc_sql_file = new FileWriter(rollbakc_sql_file_path);//

String update_sql = "";
String rollback_sql = "";


//----code------------------------------

 

 //写入日志
update_sql = "-- 更新语句"+"\r\n";
this.writeLog(update_sql_file, update_sql);
rollback_sql = " -- 回滚语句"+"\r\n";
this.writeLog(rollbakc_sql_file, rollback_sql);

 

//----code------------------------------

//关闭日志
this.logClose(update_sql_file);
this.logClose(rollbakc_sql_file);
}

public void writeLog(FileWriter writer,String log){
try {
writer.write(log);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}

public void logClose(FileWriter writer){
if(writer!=null){
try {
writer.close();
} catch (IOException e) {
System.out.println(this+",关闭日志失败");
e.printStackTrace();
}
}
}

 

推荐阅读