Check out the AtomicFile class. It was introduced in API 17. Seems like
prior to the write, it copies the file to a backup file name, and then if
the write fails, it copies the backup back over the original file.

There are other implementations of "safe" file writing in AOSP. See
frameworks/base/com/android/internal/os/AtomicFile.java and
frameworks/base/core/java/com/android/internal/util/JournaledFile.java. You
won't be able to import these directly, but you can probably just copy the
code because it's Apache licensed.

IIRC, JournaledFile will write to a backup file until the write completes,
then moves the backup over the original file.


On Tue, Nov 27, 2012 at 4:41 PM, Toby <t...@tobiah.org> wrote:

> I am opening a file for append using BufferedWriter.  Each time the app
> is run, more data is added to the file.  At first a made a little class
> representing the file and gave it a write() method that would take a string
> and send it out, then flush() the Writer.  I was wondering when I should
> close the file, or whether I really needed to.  Will java close the file
> for me when the app goes away?  Right now, since the writes are few and
> far between, I'm just opening and closing the file each time I write
> a line to it, but that seems really awkward.
>
> Thanks,
>
> Toby
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to 
> android-developers@**googlegroups.com<android-developers@googlegroups.com>
> To unsubscribe from this group, send email to
> android-developers+**unsubscr...@googlegroups.com<android-developers%2bunsubscr...@googlegroups.com>
> For more options, visit this group at
> http://groups.google.com/**group/android-developers?hl=en<http://groups.google.com/group/android-developers?hl=en>
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to