我知道如何将文本写入文件并将其存储在Java中,但我想知道是否存在一种存储大量可以在以后从文件中检索的变量的方法,并将其作为变量放回到Java应用程序中.
我会存储数字和字符串,但是存储它们的组.
谢谢 :)
n0p*_*0pe 13
Java可以读取属性文件,就像读取地图一样.这为您提供了一种使用键/值系统存储信息的简便方法.
查看http://download.oracle.com/javase/6/docs/api/java/util/Properties.html
使用XML!您将使您的变量可移植.即使你的咖啡机也能使用它们.
一个SAX解析器是建立在Java中.
这是一个用于编写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);
Run Code Online (Sandbox Code Playgroud)
这是一个用于读取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");
Run Code Online (Sandbox Code Playgroud)