代码如下:
public void saveLogToDisk(String logs) {
FileWriter writer=null;
SimpleDateFormat sdfMin = new SimpleDateFormat(
logs = "\n\n时间:" + sdfMin.format(date) + "--日志:" + logs+"\n\n";
boolean hasFile = false;
String dataPath = sdfDay.format(date);
File logFile = new File(basePath + dataPath + "airlog.txt");
File parent = new File(logFile.getParent());
if (!parent.exists()) {
boolean isMk = parent.mkdirs();
if (isMk) {
hasFile = logFile.createNewFile();
}
} else {
hasFile = logFile.createNewFile();
}
}
if (hasFile) {
writer = new FileWriter(logFile, true);
writer.write(logs);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (writer!=null) {
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void saveLogToDisk(String logs) {
FileWriter writer=null;
try {
//格式化日期
SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");SimpleDateFormat sdfMin = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
//我祥和里写的是c盘的airlog文件夹下
String basePath = "C:\\airlog\\";
//文本名称为日期加上airlog.txt
Date date = new Date();logs = "\n\n时间:" + sdfMin.format(date) + "--日志:" + logs+"\n\n";
boolean hasFile = false;
String dataPath = sdfDay.format(date);
File logFile = new File(basePath + dataPath + "airlog.txt");
hasFile = logFile.exists();
//判断c盘下的airlog的文件夹是否存在,存在的话直接将读取到的数据放进去,没有的话就新建文件夹
if (!hasFile) {File parent = new File(logFile.getParent());
if (!parent.exists()) {
boolean isMk = parent.mkdirs();
if (isMk) {
hasFile = logFile.createNewFile();
}
} else {
hasFile = logFile.createNewFile();
}
}
if (hasFile) {
writer = new FileWriter(logFile, true);
writer.write(logs);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (writer!=null) {
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}