Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3695972
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:40:56+00:00 2026-05-19T04:40:56+00:00

Tim Bray’s article Saving Data Safely left me with open questions. Today, it’s over

  • 0

Tim Bray’s article “Saving Data Safely” left me with open questions. Today, it’s over a month old and I haven’t seen any follow-up on it, so I decided to address the topic here.

One point of the article is that FileDescriptor.sync() should be called to be on the safe side when using FileOutputStream. At first, I was very irritated, because I never have seen any Java code doing a sync during the 12 years I do Java. Especially since coping with files is a pretty basic thing. Also, the standard JavaDoc of FileOutputStream never hinted at syncing (Java 1.0 – 6). After some research, I figured ext4 may actually be the first mainstream file system requiring syncing. (Are there other file systems where explicit syncing is advised?)

I appreciate some general thoughts on the matter, but I also have some specific questions:

  1. When will Android do the sync to the file system? This could be periodic and additionally based on life cycle events (e.g. an app’s process goes to the background).
  2. Does FileDescriptor.sync() take care of syncing the meta data? That is syncing the directory of the changed file. Compare to FileChannel.force().
  3. Usually, one does not directly write into the FileOutputStream. Here’s my solution (do you agree?):

    FileOutputStream fileOut = ctx.openFileOutput(file, Context.MODE_PRIVATE);
    BufferedOutputStream out = new BufferedOutputStream(fileOut);
    try {
        out.write(something);
        out.flush();
        fileOut.getFD().sync();
    } finally {
        out.close();
    }
    

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:40:57+00:00Added an answer on May 19, 2026 at 4:40 am

    Android will do the sync when it needs to — such as when the screen turns off, shutting down the device, etc. If you are just looking at “normal” operation, explicit sync by applications is never needed.

    The problem comes when the user pulls the battery out of their device (or does a hard reset of the kernel), and you want to ensure you don’t lose any data.

    So the first thing to realize: the issue is when power is suddenly lost, so a clean shutdown can not happen, and the question of what is going to happen in persistent storage at that point.

    If you are just writing a single independent new file, it doesn’t really matter what you do. The user could have pulled the battery while you were in the middle of writing, right before you started writing, etc. If you don’t sync, it just means there is some longer time from when you are done writing during which pulling the battery will lose the data.

    The big concern here is when you want to update a file. In that case, when you next read the file you want to have either the previous contents, or the new contents. You don’t want to get something half-way written, or lose the data.

    This is often done by writing the data in a new file, and then switching to that from the old file. Prior to ext4 you knew that, once you had finished writing a file, further operations on other files would not go on disk until the ones on that file, so you could safely delete the previous file or otherwise do operations that depend on your new file being fully written.

    However now if you write the new file, then delete the old one, and the battery is pulled, when you next boot you may see that the old file is deleted and new file created but the contents of the new file is not complete. By doing the sync, you ensure that the new file is completely written at that point so can do further changes (such as deleting the old file) that depend on that state.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In The Zen of Python, by Tim Peters, the sentence Complex is better than
I've been having a play with the fantastic active_directory module from Tim Golden and
Does facebook have an api for searching a user's friend? Tim
for example: [ (id=>1, email=>'tim@tim.com', name=>'tim'), (id=>2, email=>'joe@joe.com', name=>'joe'), (id=>3, email=>'dan@dan.com', name=>'dan') ] How
I have 2 tables, Table1: id, int1, int2, int3 john,1,2,4 tim,2,3,4 pete,1,3,4 Table2: integer,blob
I am looking at the example from http://www.javascriptkit.com/javatutors/oopjs.shtml var person = new Object() person.name
am new to Python packaging and am trying to work out which flags I
I have a string of key->value pairs in the following colon-separated format: MIME-Version: 1.0
I use the gplots package to output barplots. I use it inside a for-loop,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.