【发布时间】:2014-08-31 23:33:11
【问题描述】:
我正在将日志保存到 sdcard 上的 .txt 文件中,但是一旦保存了两行,它就会覆盖它并重新开始?
这是我的代码:
public static String getTimestamp() {
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("MMMdd HH:mm:ss", Locale.getDefault());
String currentTimeStamp = dateFormat.format(new Date()); // Find todays date
return currentTimeStamp;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void writeToLog(Context context, String string) {
String text = getTimestamp() + " " + string;
// ONLY SAVES TWO LINES
try {
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(logFile, true)));
out.println(text);
out.close();
} catch (IOException e) {
Log.d(Constants.APP_NAME, e.toString());
}
}
在恢复中挂载 /data 后,/sdcard 和 /data/media/0 中的日志文件会显示完整的日志历史记录,但不会在设备开机时显示
【问题讨论】:
-
之前没有使用过 PrintWriter,但您是否尝试过使用 print 而不是 println
-
@zgc7009 我认为这不是问题,因为操作员说两行已保存,但它会覆盖并加注星号
-
我想知道
PrintWriter中是否丢失了异常。调用out.checkError(),看看是否返回true。 -
你试过 printWriter.append() 吗?
-
@arayray 作为问题发布者,您应该能够对发布在您问题上的任何答案发表评论。
标签: java android bufferedwriter