我有一个日志记录类,用于写入应用程序内部存储空间中的文件。只要日志文件超过大小限制。为了清除内容,我关闭了当前的FileOutputStream并使用Write模式创建了一个新的流,并关闭了它。有没有更好的方法可以完成此任务:

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();
                }
            }
        }
    }

最佳答案

您也可以不覆盖任何内容。

更新:

getFilesDir ()似乎有更好的选择。看看这个问题How to delete internal storage file in android?

08-18 05:20
查看更多