简体   繁体   English

如何备份android中的电池插件数据?

[英]How to backup data on battery plug off in android?

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? java会抛出损坏的文件异常,还是说read arraylist为null还是未知对象?

PS. PS。 maybe create another file that will keep the MD5 checksum of the data file? 也许创建另一个文件,该文件将保留数据文件的MD5校验和? 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. 我想要一种尽可能轻量的方法,因为在数千个属性集的每个属性更改上读取/写入对存储的更改,我已经过多地使用了CPU。 Probably a database would have been a better idea. 可能数据库本来是个更好的主意。

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. 我不能说Java将如何读取损坏的序列化数组,但是为了安全起见,我们假设没有错误检测。

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). 两种情况都将以相同的方式工作,尽管第一种方法可能会更快一些,因为在将任何内容写入磁盘之前都计算了校验和(因此避免了额外的文件I / O循环)。

As you mentioned, MD5 would be fine for this. 如你所述, MD5对此没问题。 (Even a basic CRC would probably be fine -- you don't need a cryptograhpic hash for this.) (即使是基本的CRC也可能没问题-您不需要加密哈希)。

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. 是的,此时您可能只想使用Android内置的SQLite功能。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM