[英]how to store persistent data in java
I am very new to java and am making some assumptions based on what I have read, so please correct me if I am wrong. 我对Java还是很陌生,并且根据我所读的内容做出了一些假设,所以如果我错了,请更正我。
I'm making an Android app that downloads JSON data from the SD card, which you can then search by filtering. 我正在制作一个Android应用程序,该程序可从SD卡下载JSON数据,然后可以通过过滤进行搜索。
If I were doing this in javascript, I'd just save the data to global variable and access it whenever. 如果我使用javascript进行此操作,则只需将数据保存到全局变量并随时访问。 But (from the little I have read), java doesn't have globals and what it does have that are similar aren't really wise to use.
但是(从我所读的内容中),java没有全局变量,并且它所具有的相似之处并不是真正明智的选择。
So what I do now is that every time a new search begins, is I download all the data again from the SD card. 因此,我现在要做的是,每次开始新的搜索时,都是从SD卡重新下载所有数据。 But this seems horribly inefficient.
但这似乎效率很低。 I have read a little bit about Singletons and Builders and I guess maybe that's what I'm looking for (I'm really stumbling around in the dark here), but I don't really understand how to implement them in this case, or if they are in fact the best solution.
我已经阅读了有关Singletons和Builders的一些文章,我想也许这就是我想要的(我真的在这里陷入困境),但是我不太了解在这种情况下如何实现它们,或者如果它们实际上是最好的解决方案。
If it makes any difference, the data is never going to change while the app is running. 如果有什么不同,则应用程序运行时数据永远不会改变。
Could somebody give me some pointers on the best way to do this? 有人可以给我一些最好的方法吗? I really just want to get the data once when the app loads, and then be able to access it whenever later.
我真的只想在应用加载时获取一次数据,然后以后再访问它。 If it helps, this is the code that I use to get the data from the SD card...
如果有帮助,这就是我用来从SD卡获取数据的代码...
public static JSONArray getArray(){
FileInputStream stream = null;
String jString = null;
File yourFile = new File("/mnt/extSdCard/app/thejson.json");
try {
stream = new FileInputStream(yourFile);
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
/* Instead of using default, pass in a decoder. */
jString = Charset.defaultCharset().decode(bb).toString();
} catch (FileNotFoundException e) {
Log.i("err", "file not found");
}
catch (IOException e) {
Log.i("err", "IO exception");
}
try {
stream.close();
} catch (IOException e) {
Log.i("1", "IO exception");
}
try {
names = new JSONArray(jString);
} catch (JSONException e1) {
Log.i("err", "IO exception");
}
return names;
}
You can save your data in a few different ways in Java. 您可以在Java中以几种不同的方式保存数据。 If you want to share data amongst many different classes, you can create a variable in one class and call it public, or you can create a public static return method which returns the desired value (in this case the variable is private).
如果要在许多不同的类之间共享数据,则可以在一个类中创建一个变量并将其命名为public,也可以创建一个公共的静态返回方法,该方法返回所需的值(在这种情况下,该变量为private)。 A static method can be accessed regardless of whether a class contains the object which contains the method.
无论类是否包含包含该方法的对象,都可以访问静态方法。 Another way to store your data, although I do not recommend this, I recommend the static method, is to create an interface and have all classes using the data implement that interface.
存储数据的另一种方法(尽管我不建议这样做,但我建议使用静态方法)是创建一个接口,并使所有使用数据的类都实现该接口。
只要在您的主要活动中将“名称” JSON Array设置为成员变量,就可以在活动有效期间对其进行访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.