简体   繁体   English

在 Java 中存储数据的最佳方式,如 pickle

[英]best way to store data in Java like pickle

Basically, I just want to save two integeres into a File, so that i can reuse them the next time the programm starts.基本上,我只想将两个整数保存到一个文件中,以便下次程序启动时可以重用它们。 Id like to do it like pickle in python, beacuse writing it just into a txt file is cumbersome.我喜欢像 python 中的 pickle 那样做,因为只将它写入 txt 文件很麻烦。 I have read some articles and other questions wehere they say I should use Java serializatio or XML or JSON, but Im not sure wheter that is the right thing in my case.我已经阅读了一些文章和其他问题,他们说我应该使用 Java 序列化或 XML 或 JSON,但我不确定这对我来说是否正确。 Id like to use the easiest way.我想使用最简单的方法。 thank you very much in advance for trying to solve my problem!非常感谢您尝试解决我的问题! <3 <3

You could use serialization, XML or JSON (usually with additional libraties).您可以使用序列化、XML 或 JSON(通常带有附加库)。 An easy way to store configuration data in files is by using Java property files which are supported by the JRE without any additional dependencies.将配置数据存储在文件中的一种简单方法是使用 JRE 支持的 Java 属性文件,而无需任何其他依赖项。 Property files are text files and have a simple key=value syntax, see below.属性文件是文本文件,具有简单的key=value语法,请参见下文。 To write two values to a property file you can do要将两个值写入属性文件,您可以执行以下操作

  String prop1 = "foo";
  String prop2 = "bar";

  try (OutputStream output = new FileOutputStream("config.properties")) {

      Properties prop = new Properties();

      // set the properties value
      prop.setProperty("prop1", prop1);
      prop.setProperty("prop2", prop2);

      // save properties to project root folder
      prop.store(output, "my app's config file");
  } catch (IOException io) {
      io.printStackTrace();
      // TODO: improve error handling
  }

which should give you something like这应该给你类似的东西

#my app's config file
#Sat Feb 29 12:29:27 CET 2020
prop2=bar
prop1=foo

And to load it:并加载它:

try (InputStream input = new FileInputStream("config.properties")) {

Properties prop = new Properties();

// load a properties file
prop.load(input);

// get the property value and print it out
String prop1 = prop.getProperty("prop1");
String prop2 = prop.getProperty("prop2");

System.out.println("prop1 = " + prop1);
System.out.println("prop2 = " + prop2);

} catch (IOException ex) {
  ex.printStackTrace();
  // TODO: improve error handling
}

For integer values you would need some type conversion, eg the first two lines would be对于整数值,您需要进行一些类型转换,例如前两行是

String prop1 = Integer.toString(23);
String prop2 = Integer.toString(42);

and reading the properties then becomes然后读取属性就变成了

int prop1 = Integer.parseInt(prop.getProperty("prop1"));
int prop2 = Integer.parseInt(prop.getProperty("prop2"));

This solution does not scale well in case the number of properties increases or there are frequent changes.如果属性数量增加或频繁更改,则此解决方案不能很好地扩展。 For a more generic procedure, see this post: Get int, float, boolean and string from Properties有关更通用的过程,请参阅此帖子: 从属性中获取 int、float、boolean 和 string

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

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