如何在Android中的电池拔掉时备份数据?

发布于 2024-12-23 04:37:49 字数 364 浏览 1 评论 0原文

我正在开发一个将数据写入文件的应用程序。让我们假设在写入数据时我们拔掉了电池。该文件会发生什么情况?它会写一半(已损坏)、空还是与我们写入之前相同?我的猜测是它会被损坏。当文件存储对象的数组列表时,如何在重新启动手机时检查它是否损坏? java会抛出损坏的文件异常或说读取的数组列表为空或它是未知对象吗?

附言。也许创建另一个文件来保存数据文件的 MD5 校验和?每当我首先写入文件数据时,我都会生成其校验和,然后当我从数据文件中读取时,会生成校验和并将其与前一个进行比较。这将表明我的数据是否完好无损,但它不允许我回滚到以前的状态(损坏前的状态)。我想要一种尽可能轻量级的方法,通过在数千个属性集的每个属性更改上读取/写入对我的存储的更改,我已经过度使用了 CPU。也许数据库会是一个更好的主意。

I am developing an application that is writing data to a file. Lets assume while it is writing the data we plug off the battery. What will happen to the file? will it be half-writen (corrupted), empty or same as before we wrote to it? My guess is that it will be corrupted. How to check if it is corrupted when we restart the phone when the file was storing an arraylist of objects? will java throw a corrupted file exception or say that the read arraylist is null or that it is an unknown object?

PS. maybe create another file that will keep the MD5 checksum of the data file? And whenever I write to the file data first I produce its checksum and then when I read from the data file produce a checksum and compare it with the previous. That will indicate whether my data are intact but it wont allow me to roll back to a previous state (pre-corrupted one). I would like a method that would be as lightweight as possible, I am already using the CPU too much by reading/writing changes to my storage on every attribute change of a set of thousands. Probably a database would have been a better idea.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生路 2024-12-30 04:37:49

我无法说明 Java 将如何读取损坏的序列化数组,但为了安全起见,我们假设没有错误检测。

在这种情况下,您有两个简单的选择:

  • 在序列化数据之前,将数据的校验和存储在数据结构中。
  • 计算最终序列化文件的校验和。

任何一种情况都会以相同的方式工作,尽管第一个选项可能会更快一点,因为您在将任何内容写入磁盘之前计算校验和(因此避免了额外的文件 I/O 轮)。

正如您所提到的, MD5 就可以了。 (即使是基本的 CRC 也可能没问题——你不需要为此提供密码哈希.)

如果您想允许回滚到以前的版本 - 我只需将每个版本存储为单独的文件,然后有一个指向最新版本的指针。 (如果您在写入操作的最后一步更新指针,这还将提供额外级别的保护,防止将损坏的数据输入到您的应用程序中 - 尽管您也必须准备好该指针也将被损坏。由于这本质上是一个提交步骤,因此您可以将损坏的指针解释为“使用最新版本”。)

是的,此时您可能只想使用 Android 中内置的 SQLite 功能。 :)

I can't say how Java will read in a corrupted serialized array, but for safety let's assume that there's no error detection.

In that case, you have two easy options:

  • Store a checksum of your data inside your data structure, before you serialize it.
  • Compute the checksum of the final serialized file.

Either case will work the same way, though the first option might be a bit faster since you compute the checksum before you've written anything to disk (and therefor avoid an extra round of file I/O).

As you mentioned, MD5 would be fine for this. (Even a basic CRC would probably be fine -- you don't need a cryptograhpic hash for this.)

If you want to allow rolling back to a previous version -- I'd just store each version as a separate file and then have a pointer to the most recent one. (If you update the pointer as the last step of your write operation, this will also provide an extra level of protection against corrupt data being input to your app -- though you'll have to prepare for this pointer to be corrupt as well. Since this is essentially a commit step, you could interpret a corrupt pointer as "use the last version".)

And yes, at this point you might want to just use the SQLite functionality built into Android. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文