亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android中怎么保證數據同步寫入磁盤

發布時間:2021-08-10 17:08:15 來源:億速云 閱讀:149 作者:Leah 欄目:編程語言

這期內容當中小編將會給大家帶來有關Android中怎么保證數據同步寫入磁盤,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

在一些特定的工作場景中,我們把數據及時寫出磁盤,而不是暫時保存在系統的文件緩存區,防止掉電導致數據丟失

/** * Force all system buffers to synchronize with the underlying * device. This method returns after all modified data and * attributes of this FileDescriptor have been written to the * relevant device(s). In particular, if this FileDescriptor * refers to a physical storage medium, such as a file in a file * system, sync will not return until all in-memory modified copies * of buffers associated with this FileDescriptor have been * written to the physical medium. * * sync is meant to be used by code that requires physical * storage (such as a file) to be in a known state For * example, a class that provided a simple transaction facility * might use sync to ensure that all changes to a file caused * by a given transaction were recorded on a storage medium. * * sync only affects buffers downstream of this FileDescriptor. If * any in-memory buffering is being done by the application (for * example, by a BufferedOutputStream object), those buffers must * be flushed into the FileDescriptor (for example, by invoking * OutputStream.flush) before that data will be affected by sync. * * @exception SyncFailedException *    Thrown when the buffers cannot be flushed, *    or because the system cannot guarantee that all the *    buffers have been synchronized with physical media. * @since   JDK1.1 */public native void sync() throws SyncFailedException;

可能一看到這個場景,很多人會想到數據庫的事務,查看Android數據庫sqlite的源碼可以看到,數據庫事務只能保證n個操作,要么都執行,要么都不執行。數據庫事務在所有操作完成后,會提醒文件系統與磁盤同步,但是不會等到所有系統緩沖區與磁盤同步完成才返回!

FileDescriptor.getFd().sync();會強制所有系統緩沖區與磁盤同步File file = new File("/sdcard/a.txt");    try {      FileOutputStream outputStream = new FileOutputStream(file);      outputStream.write("kuangxf".getBytes());      outputStream.flush();      //強制文件系統刷新      outputStream.getFD().sync();    } catch (Exception e) {      e.printStackTrace();    }

上述就是小編為大家分享的Android中怎么保證數據同步寫入磁盤了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

衡阳县| 崇仁县| 阳谷县| 佛山市| 昌乐县| 石狮市| 玛纳斯县| 茶陵县| 和顺县| 长沙县| 大荔县| 铅山县| 平远县| 东乡族自治县| 思茅市| 阿瓦提县| 崇阳县| 花莲县| 绥芬河市| 紫阳县| 宝清县| 噶尔县| 长治县| 焦作市| 剑阁县| 玛多县| 楚雄市| 南投市| 华池县| 普定县| 陆河县| 崇义县| 七台河市| 清河县| 易门县| 九龙坡区| 枣庄市| 朔州市| 鹤峰县| 长岭县| 云南省|