在項目中使用xml配置文件存儲信息
在項目中經常會使用一些系統級的配置信息,那這寫信息以什么方式存儲,又是一什么放方式讀取的。在這里只是簡單討論一下使用xml文件存儲的方式。
近期項目要求編寫同一的文件上傳路徑,并提供可修改的配置接口。這里我使用的是xml文件配置方式。代碼如下:
配置文件:
Java代碼 :
<?xml version="1.0" encoding="UTF-8"?>
<config>
<!--文件上傳的路徑-->
<upload_path>/tmp/</upload_path>
</config>
對配置文件讀寫的util類:
Java代碼:
public class Config {
// 解析config.xml配置文件 ,返回文件存儲路徑
public String getUploadPath() throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
String configPath = this.getClass().getResource("/").getPath() + File.separator + "config.xml";
Document document = builder.parse(configPath);
NodeList nodeList = document.getElementsByTagName("upload_path");
Node node = nodeList.item(0);
return node.getTextContent();
}
}
行業門戶(http://www.software8.co/software/wangzhantuiguangruanjian/)文章,希望大家可以留言建議