Skip to content Skip to sidebar Skip to footer

Edit File From Internal Storage

How can I edit the content of a file located on the internal storage in my Android app. I want to erase the whole content and then write to the file again, instead of appending dat

Solution 1:

Your class is already doing what you rquire. It first erases the contents of the file and then writes on it. For further understanding,

When you initiate your stream with MODE_PRIVATE, the second time when you try to write the file, the contents that are already in the file gets erased and the new contents are written.

outputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);

When you use MODE_APPEND, the contents that are already there stays and the new contents will be appended to the file.

outputStream = context.openFileOutput(fileName, Context.MODE_APPEND);

For more reference and detailed knowledge on dealing with files in Internal storage, I recommend you to watch the below three short videos which gives you detailed description with demo.

http://www.youtube.com/watch?v=Jswr6tkv8ro&index=4&list=PLonJJ3BVjZW5JdoFT0Rlt3ry5Mjp7s8cThttp://www.youtube.com/watch?v=cGxHphBjTBk&index=5&list=PLonJJ3BVjZW5JdoFT0Rlt3ry5Mjp7s8cThttp://www.youtube.com/watch?v=mMcrj_To18k&index=6&list=PLonJJ3BVjZW5JdoFT0Rlt3ry5Mjp7s8cT

Hope it helps! Any more questions, please comment below.

Solution 2:

You can just delete the file first with:

File f = new File(filename);
if(f.exists()){
    f.delete();
}

And then create a new one with same path/name and write to it. I'm assuming that your filename is the path to the file on the device.

But probably I'm not getting your real problem?

Post a Comment for "Edit File From Internal Storage"