我们一般会将Java应用的配置參数保存在属性文件里。Java应用的属性文件能够是一个正常的基于key-value对,以properties为扩展名的文件。也能够是XML文件.
在本案例中。將会向大家介绍怎样通过Java程序输出这两种格式的属性文件。并介绍怎样从classpath中载入和使用这两种属性文件。
以下是案例程序代码:
PropertyFilesUtil.java
package com.journaldev.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Set;
public class PropertyFilesUtil {
public static void main(String[] args) throws IOException {
String propertyFileName = "DB.properties";
String xmlFileName = "DB.xml";
writePropertyFile(propertyFileName, xmlFileName);
readPropertyFile(propertyFileName, xmlFileName);
readAllKeys(propertyFileName, xmlFileName);
readPropertyFileFromClasspath(propertyFileName);
}
/**
* read property file from classpath
* @param propertyFileName
* @throws IOException
*/
private static void readPropertyFileFromClasspath(String propertyFileName) throws IOException {
Properties prop = new Properties();
prop.load(PropertyFilesUtil.class.getClassLoader().getResourceAsStream(propertyFileName));
System.out.println(propertyFileName +" loaded from Classpath::db.host = "+prop.getProperty("db.host"));
System.out.println(propertyFileName +" loaded from Classpath::db.user = "+prop.getProperty("db.user"));
System.out.println(propertyFileName +" loaded from Classpath::db.pwd = "+prop.getProperty("db.pwd"));
System.out.println(propertyFileName +" loaded from Classpath::XYZ = "+prop.getProperty("XYZ"));
}
/**
* read all the keys from the given property files
* @param propertyFileName
* @param xmlFileName
* @throws IOException
*/
private static void readAllKeys(String propertyFileName, String xmlFileName) throws IOException {
System.out.println("Start of readAllKeys");
Properties prop = new Properties();
FileReader reader = new FileReader(propertyFileName);
prop.load(reader);
Set<Object> keys= prop.keySet();
for(Object obj : keys){
System.out.println(propertyFileName + ":: Key="+obj.toString()+"::value="+prop.getProperty(obj.toString()));
}
//loading xml file now, first clear existing properties
prop.clear();
InputStream is = new FileInputStream(xmlFileName);
prop.loadFromXML(is);
keys= prop.keySet();
for(Object obj : keys){
System.out.println(xmlFileName + ":: Key="+obj.toString()+"::value="+prop.getProperty(obj.toString()));
}
//Now free all the resources
is.close();
reader.close();
System.out.println("End of readAllKeys");
}
/**
* This method reads property files from file system
* @param propertyFileName
* @param xmlFileName
* @throws IOException
* @throws FileNotFoundException
*/
private static void readPropertyFile(String propertyFileName, String xmlFileName) throws FileNotFoundException, IOException {
System.out.println("Start of readPropertyFile");
Properties prop = new Properties();
FileReader reader = new FileReader(propertyFileName);
prop.load(reader);
System.out.println(propertyFileName +"::db.host = "+prop.getProperty("db.host"));
System.out.println(propertyFileName +"::db.user = "+prop.getProperty("db.user"));
System.out.println(propertyFileName +"::db.pwd = "+prop.getProperty("db.pwd"));
System.out.println(propertyFileName +"::XYZ = "+prop.getProperty("XYZ"));
//loading xml file now, first clear existing properties
prop.clear();
InputStream is = new FileInputStream(xmlFileName);
prop.loadFromXML(is);
System.out.println(xmlFileName +"::db.host = "+prop.getProperty("db.host"));
System.out.println(xmlFileName +"::db.user = "+prop.getProperty("db.user"));
System.out.println(xmlFileName +"::db.pwd = "+prop.getProperty("db.pwd"));
System.out.println(xmlFileName +"::XYZ = "+prop.getProperty("XYZ"));
//Now free all the resources
is.close();
reader.close();
System.out.println("End of readPropertyFile");
}
/**
* This method writes Property files into file system in property file
* and xml format
* @param fileName
* @throws IOException
*/
private static void writePropertyFile(String propertyFileName, String xmlFileName) throws IOException {
System.out.println("Start of writePropertyFile");
Properties prop = new Properties();
prop.setProperty("db.host", "localhost");
prop.setProperty("db.user", "user");
prop.setProperty("db.pwd", "password");
prop.store(new FileWriter(propertyFileName), "DB Config file");
System.out.println(propertyFileName + " written successfully");
prop.storeToXML(new FileOutputStream(xmlFileName), "DB Config XML file");
System.out.println(xmlFileName + " written successfully");
System.out.println("End of writePropertyFile");
}
}
当执行这段代码时。writePropertyFile 方法会在生成上述两种格式的属性文件,并將文件存储在project的根文件夹下。
writePropertyFile 方法生成的两种属性文件内容:
DB.properties
#DB Config file
#Fri Nov 16 11:16:37 PST 2012
db.user=user
db.host=localhost
db.pwd=password
DB.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>DB Config XML file</comment>
<entry key="db.user">user</entry>
<entry key="db.host">localhost</entry>
<entry key="db.pwd">password</entry>
</properties>
须要注意的是comment元素,我们在使用prop.storeToXML(new FileOutputStream(xmlFileName), "DB Config XML file");
这段代码时第二个參数传入的是凝视内容,假设传入null,生成的xml属性文件將没有comment元素。
控制台输出内容例如以下:
Start of writePropertyFile
DB.properties written successfully
DB.xml written successfully
End of writePropertyFile
Start of readPropertyFile
DB.properties::db.host = localhost
DB.properties::db.user = user
DB.properties::db.pwd = password
DB.properties::XYZ = null
DB.xml::db.host = localhost
DB.xml::db.user = user
DB.xml::db.pwd = password
DB.xml::XYZ = null
End of readPropertyFile
Start of readAllKeys
DB.properties:: Key=db.user::value=user
DB.properties:: Key=db.host::value=localhost
DB.properties:: Key=db.pwd::value=password
DB.xml:: Key=db.user::value=user
DB.xml:: Key=db.host::value=localhost
DB.xml:: Key=db.pwd::value=password
End of readAllKeys
Exception in thread "main" java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at com.journaldev.util.PropertyFilesUtil.readPropertyFileFromClasspath(PropertyFilesUtil.java:
查看全文
【环境搭建】安装pyQt5 在pycharm报This application failed to start because no Qt platform plugin could be initialized的问题
【嵌入式】arduino IDE串口监视器可以正常使用但其他软件发送串口指令没有反应的问题
【操作系统】bat文件 系统找不到文件路径
十天冲刺:第二天
十天冲刺:第一天
项目需求分析NABCD电梯演讲
项目需求分析与建议-NABCD模型
团队开发之团队介绍
会议1.7
- 最新文章
-
React练习 12:02_06_鼠标移入移出改变图片透明度
React练习 11:02_05_函数传参改变Div任意属性的值
React练习 10:弹出层效果
React练习 9 :求出数组中所有数字的和
axios 安装使用
vuex 的安装及使用
Promise 是什么
vuex请求本地json数据还没完成 页面就加载渲染了
Vuex中的数据在 F5刷新后 数据消失的问题
用 vue 加载 json 的步骤
- 热门文章
-
vue 基本指令
vue cli3 获取本地json数据的方式
关于获取坐标的一些记录 ——分析淘宝放大镜效果的原理
组件传值的几种方式 和遇到的一些坑
【项目管理】《IT项目管理》Kathy Schwalbe 第1章 总论
【软件测试】《软件测试理论与实践》杜小智 第1章 软件测试概述
【计组】《计算机组成与体系结构性能设计》William Stallings 第1部分 概述 第1章 导论 introduction
【python】QT5 cvimg 转 pixmap
【数据库】计算机三级考试《数据库技术》知识点总结——5 6 7 8章
【数据库】计算机三级考试《数据库技术》知识点总结——1 2 3 4章