Java:将变量存储在文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6310415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:19:30  来源:igfitidea点击:

Java: Store variables in file

javafile

提问by Humphrey

I know how to write text to a file and store it in Java, but I was wondering if there is a way of storing lots of variables that can be retrieved from the file at a later date, and put back into the Java application as variables.

我知道如何将文本写入文件并将其存储在 Java 中,但我想知道是否有一种方法可以存储大量可以在以后从文件中检索的变量,并将其作为变量放回 Java 应用程序中.

I would be storing numbers and strings, but groups of them.

我将存储数字和字符串,但它们是一组。

Thanks :)

谢谢 :)

回答by n0pe

Java can read properties files like it reads a map. This gives you an easy way of storing information using a key/value system.

Java 可以像读取地图一样读取属性文件。这为您提供了一种使用键/值系统存储信息的简单方法。

Check out http://download.oracle.com/javase/6/docs/api/java/util/Properties.html

查看http://download.oracle.com/javase/6/docs/api/java/util/Properties.html

回答by Costis Aivalis

Use XML! You will make your variables portable. Even your coffee machine will be able to use them.

使用 XML!您将使您的变量可移植。甚至您的咖啡机也可以使用它们。

A SAX parseris built in Java.

一个SAX解析器是建立在Java中。

Here is a Java snippet for writing an XML file:

这是用于编写 XML 文件的 Java 片段:

public void saveToXML(String xml) {
    Document dom;
    Element e = null;
    // get an instance of factory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
        // using a factory get an instance of document builder
        DocumentBuilder db = dbf.newDocumentBuilder();
        // create an instance of DOM
        dom = db.newDocument();
        // create the root element
        Element rootElement = dom.createElement("myparameters");
        // create data elements and place them under root
        e = dom.createElement("brightness");
        e.appendChild(dom.createTextNode(brightness));
        rootElement.appendChild(e);

Here is a Java snippet for reading an XML file:

这是用于读取 XML 文件的 Java 片段:

public boolean loadFromXML(String xml) {    
    Document dom;
    // get an instance of the factory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
        // using factory get an instance of the document builder
        DocumentBuilder db = dbf.newDocumentBuilder();
        // parse using builder to get DOM representation of the XML file
        dom = db.parse(xml);
        Element doc = dom.getDocumentElement();
        // get the data 
        brightness = getTextValue(brightness, doc, "brightness");
        contrast = getTextValue(contrast, doc, "contrast");

回答by Talha Ahmed Khan

Wel the best way to do this is to make an XML file. It can store any object in any sort of collection.

最好的方法是制作一个 XML 文件。它可以将任何对象存储在任何类型的集合中。

I guess the XML is the best way to keep it and retrieve it later on.

我想 XML 是保存它并在以后检索它的最佳方式。