我正在将日志保存到 SD 卡上的 .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 中的日志文件就会显示完整的日志历史记录,但设备开机时不会显示
你可以简单地使用FileWriter http://developer.android.com/reference/java/io/FileWriter.html没有其他作家的束缚。一定要打电话flush()
,因为这确保将所有缓冲内容写入文件。
如果这仍然不起作用,请检查错误输出。
public static void writeToLog(Context context, String string) {
String text = getTimestamp() + " " + string + "\n"; //newline here
Writer out;
try {
out = new FileWriter(logFile, true);
out.append(text);
out.flush(); //ensure that all buffered characters are written to the target
} catch (IOException e) {
Log.d(Constants.APP_NAME, e.toString());
} finally {
if (out != null) {
out.close(); //ensure writer is closed
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)