我有一个记录类,用于写入应用程序内部存储空间中的文件。每当日志文件超出大小限制时。为了清除内容,我正在关闭当前的FileOutputStream并使用Write模式创建一个新的流并关闭它。有没有更好的方法来完成这个:在Android上清除内部存储中的文件内容
public final void clearLog() throws IOException {
synchronized (this) {
FileOutputStream fos = null;
try {
// close the current log file stream
mFileOutputStream.close();
// create a stream in write mode
fos = mContext.openFileOutput(
LOG_FILE_NAME, Context.MODE_PRIVATE);
fos.close();
// create a new log file in append mode
createLogFile();
} catch (IOException ex) {
Log.e(THIS_FILE,
"Failed to clear log file:" + ex.getMessage());
} finally {
if (fos != null) {
fos.close();
}
}
}
}
如何覆盖我以附加模式打开文件?感谢您的建议。 – ssk
嗯,是的,我的意思是在'MODE_PRIVATE'中打开,以覆盖内容。你是对的,我认为你做对了。 –
查看更新。 –