配置文件管理:Java如何读写配置文件

原创 深碍√TFBOYSˉ_ 2024-11-14 05:00 58阅读 0赞

在Java中,读写配置文件是一个常见的需求,尤其是在需要存储应用程序设置或用户偏好时。Java提供了多种方式来处理配置文件,以下是一些常用的方法:

1. 使用Properties类Properties类是Java提供的一个用于处理属性文件(通常是.properties文件)的类。属性文件是一种简单的键值对集合,每行一个键值对,键和值之间用等号(=)分隔。

读取Properties文件```javaimport java.io.FileInputStream;

import java.io.IOException;
import java.util.Properties;

public class PropertiesExample {
public static void main(String[] args) {
Properties prop = new Properties();
try (FileInputStream in = new FileInputStream(“config.properties”)) {
prop.load(in);
String value = prop.getProperty(“key”);
System.out.println(value);
} catch (IOException e) {
e.printStackTrace();
}
}
}
####写入Properties文件javaimport java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertiesExample {
public static void main(String[] args) {
Properties prop = new Properties();
prop.setProperty(“key”, “value”);
try (FileOutputStream out = new FileOutputStream(“config.properties”)) {
prop.store(out, “Configuration settings”);
} catch (IOException e) {
e.printStackTrace();
}
}
}
``###2. 使用XML文件对于更复杂的配置,可以使用XML文件。Java提供了DocumentBuilderFactoryDocumentBuilder`来解析和创建XML文件。

读取XML文件```javaimport javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import java.io.StringReader;

public class XMLConfigReader {
public static void main(String[] args) throws Exception {
String xml = “\n” +
\n” +
“;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(xml)));
document.getDocumentElement().normalize();
NodeList nodeList = document.getElementsByTagName(“setting”);
for (int i =0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
String name = element.getAttribute(“name”);
String value = element.getAttribute(“value”);
System.out.println(“Name: “ + name + “, Value: “ + value);
}
}
}
}
####写入XML文件javaimport javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class XMLConfigWriter {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Element root = document.createElement(“config”);
document.appendChild(root);
Element setting = document.createElement(“setting”);
setting.setAttribute(“name”, “key”);
setting.setAttribute(“value”, “value”);
root.appendChild(setting);
DOMSource source = new DOMSource(document);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
OutputStream out = new FileOutputStream(“config.xml”);
StreamResult result = new StreamResult(out);
transformer.transform(source, result);
out.close();
}
}
``###3. 使用JSON文件对于现代应用程序,JSON文件也是一个流行的选择。你可以使用org.json库或Gson`库来处理JSON文件。

读取JSON文件```javaimport org.json.JSONObject;

import java.nio.file.Files;
import java.nio.file.Paths;

public class JSONConfigReader {
public static void main(String[] args) throws Exception {
String content = new String(Files.readAllBytes(Paths.get(“config.json”)));
JSONObject obj = new JSONObject(content);
String value = obj.getString(“key”);
System.out.println(value);
}
}
####写入JSON文件javaimport org.json.JSONObject;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class JSONConfigWriter {
public static void main

文章版权声明:注明蒲公英云原创文章,转载或复制请以超链接形式并注明出处。

发表评论

表情:
评论列表 (有 0 条评论,58人围观)

还没有评论,来说两句吧...

相关阅读

    相关 JavaProperties配置文件

    Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集。不过Properties有特殊的地方,就是它的键和值都是字符串类型。

    相关 MFC 配置文件

      读写ini文件   参考:附带的三个网页文件 1、写ini文件 把student.ini 放到C盘根目录下,路径也可以在程序里的两个函数调整 BOOL Writ