// 获取当前日期和时间
Calendar cal = Calendar.getInstance();
String fileName = cal.get(Calendar.YEAR) + "_" + cal.get(Calendar.MONTH) + "_"
+ cal.get(Calendar.DAY_OF_MONTH) + ".txt";
FileWriter fileWriter = new FileWriter(new File(filePath + "/" + fileName), true); String log = "restart at: " + cal.get(Calendar.HOUR_OF_DAY) + ":"
+ cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND); BufferedWriter bufferWriter = new BufferedWriter(fileWriter);
// attention 换行需要以下两个一起才会在.txt文件中生效
bufferWriter.write(log + "\r\n");
bufferWriter.newLine(); bufferWriter.close();
fileWriter.close();
如代码 在输出字符串到.txt文件的时候 换行需要"\r\n"和 newLine() 一起用才有换行效果