Appending to file internal storage android

android, file-io, internal-storage

Solution

As suggested by Squonk, you can manually create your FileOutputStream in append mode with:

FileOutputStream(File file, boolean append)
FileOutputStream(String path, boolean append)

Or alternatively and arguably preferably, by using the native android method:

openFileOutput(FILENAME, Context.MODE_APPEND);

Which opens a file named 'FILENAME' located in the default storage location for your app. It is usually best to let the OS decide where to store your files. Details on storage options here. It describes how to access each of the standard storage locations and methods. Which one you choose depends on what it is you are trying to store.

Problem

If you have an existing file in the internal storage in the android app, how do you append a string to the file? I have tried using FileOutPutStream.write() but that just seems to overwrite the whole file and causes the previous data to be lost.

Original source