android in practice_Making sure files are saved with sync

来源:互联网 发布:淘宝crm系统架构 编辑:程序博客网 时间:2024/06/05 12:22

You need to guarantee that file data is written to disk immediately, regardless of the filesystem in use and the platform version.

Syncing ensures that the buffer catches up with the physical disk.

You can get a FileDescriptor reference from FileOutputStream, and then sync, as shown in the next listing.

public static boolean syncStream(FileOutputStream fos) {try {if (fos != null) {try {fos.getFD().sync();} catch (IOException e) {Log.e(Constants.LOG_TAG,"Error syncing fos " + e.getMessage(), e);}return true;}return false;}

原创粉丝点击