现在的位置: 首页 > 综合 > 正文

android in practice_Making sure files are saved with sync

2018年04月13日 ⁄ 综合 ⁄ 共 457字 ⁄ 字号 评论关闭

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;
}

抱歉!评论已关闭.