在Android上清除内部存储中的文件内容 [英] Clearing File content in Internal Storage on Android

查看:1561
本文介绍了在Android上清除内部存储中的文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日志记录类,用于写入应用程序内部存储空间中的文件.只要日志文件超过大小限制.为了清除内容,我将关闭当前的FileOutputStream并使用写入"模式创建一个新的流,然后将其关闭.有没有更好的方法可以做到这一点:

I have a logging class for writing into a file in the app's internal storage space. Whenever the log file exceeds the size limit. For clearing the contents, I am closing the current FileOutputStream and creating a new stream with Write mode and closing it. Is there a way better way for accomplishing this:

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,似乎有更好的选择()看看这个问题如何删除内部在android中存储文件?

这篇关于在Android上清除内部存储中的文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆