Java&Xml教程(十)XML作为属性文件使用
我们一般会将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:
Java&Xml教程(十)XML作为属性文件使用的更多相关文章
- ComicEnhancerPro 系列教程十八:JPG文件长度与质量
作者:马健邮箱:stronghorse_mj@hotmail.com 主页:http://www.comicer.com/stronghorse/ 发布:2017.07.23 教程十八:JPG文件长度 ...
- IoC容器-Bean管理XML方式(引入外部属性文件)
IoC操作Bean管理(引入外部属性文件) 1,直接配置数据库信息 (1)配置德鲁伊连接池 (2)引入德鲁伊连接池依赖jar包 2,通过引入外部属性文件配置数据库连接池 (1)创建外部属性文件,pro ...
- java 如何读取src根目录下的属性文件
在java项目中,如何获取src根目录下的属性文件/资源文件呢? 有如下三种方式: 方式一: InputStream in = Test.class .getResourceAsStream(&quo ...
- java读取项目或包下面的属性文件方法
1.使用java.util.Properties类的load()方法 //文件在项目下.不是在包下!! InputStream in = new BufferedInputStream(newFile ...
- Java&Xml教程(十)XML作为属性文件使用
我们通常会将Java应用的配置参数保存在属性文件中,Java应用的属性文件可以是一个正常的基于key-value对,以properties为扩展名的文件,也可以是XML文件. 在本案例中,將会向大家介 ...
- Java&Xml教程(六)使用JDOM解析XML文件
JDOM 提供了非常优秀的Java XML API来更方便的读取.修改.生成XML文档.JDOM还提供了包装类供用户从SAX.DOM.STAX事件解析.STAX流解析中选择具体的实现. 在本教程中,我 ...
- Java&Xml教程(四)使用DOM方式生成XML文件
在前面的教程中,我们学习了使用DOM解析方式读取和修改XML文件内容,今天我们来学习如何使用DOM解析机制生成XML文件. 下面是我们对要生成的XML文件的具体要求: 1.根节点元素为"Em ...
- Java&Xml教程(一)简介
XML是广泛用于数据传输和存储的技术.Java语言提供个各种各样的API来解析XML,例如DOM.SAX.StAX.JAXB.也还有一些其他的API用于解析XML,例如JDOM.本教程的目的是探索使用 ...
- Java学习笔记(十)——xml
[前面的话] 学习过程中还是效率不够高,老是容易注意力不集中,着急啊.不能在这样了,要好好学习,好好努力. 学习过程中的小知识点总结,基础知识,选择阅读. [xml定义] 定义:可扩展标记语言(英语: ...
随机推荐
- pip 安装时报错Double requirement given: numpy==1.12.1....
使用pip install -r requirements.txt 命令批量安装模块时,报错: Double requirement given: numpy==1.12.1 from https:/ ...
- LeetCode OJ--Word Search **
https://oj.leetcode.com/problems/word-search/ 类似于在棋盘上走步,走过的地方不能再走,每次都可以走当前位置的上.下.左.右,问能不能走出要求的形状来. 深 ...
- 提高Android Studio运行、编译速度方案
1.安装完成后启动卡死 刚刚打开studio就卡在gradle building的界面再也不动了(去连接墙外的网下载),那么这个时候我们就需要把这个联网下载操作屏蔽掉,找到studio安装目录,找到i ...
- 关于超大binlog事件的问题
我手里维护了一个项目,其功能是用Java模拟一个MariaDB的slave库连接到主库,对从主库传输过来的binlog事件进行监听与分析 碰到一个问题是: 如果主库做了一个很大的修改操作(比方说直接d ...
- 2018年东北农业大学春季校赛 E 阶乘后的0【数论】
链接:https://www.nowcoder.com/acm/contest/93/E来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...
- BZOJ1054(搜索)
大力搜,状态用一个16位的数字表示. #include <bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i( ...
- First Bad Version - LeetCode
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- luogu P1345 [USACO5.4]奶牛的电信Telecowmunication
https://www.luogu.org/problemnew/show/1345 拆点,中间建流量为1的边,跑最小割 #include<cstdio> #include<queu ...
- ssh免秘钥登陆实现
1.用处 搭建集群或者工作中登陆跳板机经常需要做免秘钥互相登陆彼此服务器. 2. 准备工作 假设A主机10.20.0.1想通过ssh登录到B主机10.20.0.2上. 那么客户端(A主机)需要 ...
- [置顶]
zabbix发送告警
之前使用邮件和短信发送zabbix告警信息,但告警信息无法实时查看或者无法发送,故障无法及时通知运维人员. 后来使用第三方微信接口发送信息,愉快地用了一年多,突然收费了. zabbix告警一直是我的痛 ...