0

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();
                }
            }
        }
    }
tshepang
  • 12,111
  • 21
  • 91
  • 136
ssk
  • 9,045
  • 26
  • 96
  • 169

3 Answers3

2

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?

Community
  • 1
  • 1
Xavi Gil
  • 11,460
  • 4
  • 56
  • 71
  • How do I overwrite when I have the file opened in append mode? Thanks for your suggestion. – ssk Jun 07 '12 at 22:09
  • Well yes, I meant opening in `MODE_PRIVATE`so you overwrite the content. You are right and I think you are doing it right. – Xavi Gil Jun 07 '12 at 22:14
1

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. If TRUE - text append to existing file.

Raging Bull
  • 18,593
  • 13
  • 50
  • 55
Lyusten Elder
  • 3,213
  • 1
  • 14
  • 10
0
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();  
    }  
}