简体   繁体   English

我如何在没有数据库的情况下永久保存arrayList

[英]How can I save an arrayList permanently without database

How can I save an arrayList permanently without database. 如何在没有数据库的情况下永久保存arrayList。

If you could include an example it would be great. 如果您可以包括一个示例,那就太好了。

Thank you! 谢谢!

If it's just the one ArrayList and you don't have any complex ordering to your datastore, I would just save it out internal storage in binary format. 如果它只是一个ArrayList,而您对数据存储没有任何复杂的排序,那么我将以二进制格式将其保存在内部存储中。 That will save you a lot of overhead trying to encode and decode the file in a text format such as XML or JSON. 这将节省您尝试以XML或JSON之类的文本格式编码和解码文件的大量开销。

A link to check out is Data Storage . 签出的链接是Data Storage

Edit: I put together a quick and dirty github repo to give an example. 编辑:我整理了一个快速而肮脏的github仓库来举一个例子。 It's not something I would just copy and paste into a project, but it should show you the basic mechanics. 我不是要复制并粘贴到项目中,而是应该向您展示基本原理。

Edit 2: Note that all the Objects inside the ArrayList need to implement Serializable or be primitive types for this to work. 编辑2:请注意,ArrayList内的所有对象都需要实现Serializable或为基本类型才能起作用。

Export the ArrayList to a file maybe in a specific format ( Xml,JSON,csv etc. ) Then ready upon the data when you need it. 将ArrayList导出到文件中,可能采用特定格式(Xml,JSON,csv等),然后在需要时准备好数据。

Pseudo: 伪:

String data;

i = 0;
Loop Array
   data += array[i]

   i++;
End loop

Write data to file

You can make it as easy/hard as you want ofc. 您可以根据需要轻松/轻松地实现它。 But you get the idea i think :) 但是你明白我的想法:)

Have you seen this example ? 你看过这个例子吗?

But you will lose data after a switchoff or restart. 但是在关闭或重新启动后,您将丢失数据。

You can use the Serialize stuff from Java. 您可以使用Java中的Serialize东西。 The objects inside the ArrayList should implement the Serializble Object. ArrayList内部的对象应实现Serializble对象。 Than, you can save the resulting binary file to storage and reload it. 然后,您可以将生成的二进制文件保存到存储器并重新加载。 here's an example of how to do it: http://www.jondev.net/articles/Android_Serialization_Example_%28Java%29 这是一个操作方法示例: http : //www.jondev.net/articles/Android_Serialization_Example_%28Java%29

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

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