简体   繁体   English

在Android上清除内部存储中的文件内容

[英]Clearing File content in Internal Storage on Android

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. 为了清除内容,我关闭了当前的FileOutputStream并使用Write模式创建了一个新的流,并关闭了它。 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();
                }
            }
        }
    }

You could also overwrite your file with nothing. 您也可以不覆盖任何内容。

UPDATE : 更新

There seems to be a better option with getFilesDir () Have a look at this question How to delete internal storage file in android? 似乎有一个更好的选择与getFilesDir()看一下这个问题如何在android中删除内部存储文件?

Write empty data into file: 将空数据写入文件:

String string1 = "";
        FileOutputStream fos ;
        try {
            fos = new FileOutputStream("/sdcard/filename.txt", false);
            FileWriter fWriter;

            try {
                fWriter = new FileWriter(fos.getFD());

                fWriter.write(string1);
                fWriter.flush();
                fWriter.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                fos.getFD().sync();
                fos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

In this code: 在此代码中:

fos = new FileOutputStream("/sdcard/filename.txt", false);

FALSE - for write new content. FALSE用于编写新内容。 If TRUE - text append to existing file. 如果为TRUE文本追加到现有文件。

public void writetofile(String text){ // text is a string to be saved    
   try {                               
        FileOutputStream fileout=openFileOutput("mytextfile.txt", false); //false will set the append mode to false         
        OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);  
        outputWriter.write(text);  
        outputWriter.close();  
        readfromfile();  
        Toast.makeText(getApplicationContext(), "file saved successfully",  
                Toast.LENGTH_LONG).show();  
        }catch (Exception e) {  
            e.printStackTrace();  
    }  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM