重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要介绍了XML作为属性文件的使用,具有一定借鉴价值,需要的朋友可以参考下。下面就和我一起来看看吧。
创新互联是一家专注于成都做网站、网站建设与策划设计,镇坪网站建设哪家好?创新互联做网站,专注于网站建设十年,网设计领域的专业建站公司;建站业务涵盖:镇坪等地区。镇坪做网站价格咨询:18980820575
我们通常会将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
当运行这段代码时,writePropertyFile 方法会在生成上述两种格式的属性文件,并將文件存储在工程的根目录下。
writePropertyFile 方法生成的两种属性文件内容:
DB.properties
#DB Config file#Fri Nov 16 11:16:37 PST 2012db.user=user db.host=localhost db.pwd=password
DB.xml
DB Config XML file user localhost password
需要注意的是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 readPropertyFileDB.properties::db.host = localhostDB.properties::db.user = userDB.properties::db.pwd = passwordDB.properties::XYZ = nullDB.xml::db.host = localhostDB.xml::db.user = userDB.xml::db.pwd = passwordDB.xml::XYZ = null End of readPropertyFile Start of readAllKeysDB.properties:: Key=db.user::value=userDB.properties:: Key=db.host::value=localhostDB.properties:: Key=db.pwd::value=passwordDB.xml:: Key=db.user::value=userDB.xml:: Key=db.host::value=localhostDB.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:31) at com.journaldev.util.PropertyFilesUtil.main(PropertyFilesUtil.java:21)
这里报了空指针异常,原因是生成的文件保存在工程的根目录下面,而读取时是从classpath下读取,將上面生成的两个属性文件拷贝到src下再次运行程序即可。
我们通常会将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
当运行这段代码时,writePropertyFile 方法会在生成上述两种格式的属性文件,并將文件存储在工程的根目录下。
writePropertyFile 方法生成的两种属性文件内容:
DB.properties
#DB Config file#Fri Nov 16 11:16:37 PST 2012db.user=user db.host=localhost db.pwd=password
DB.xml
userlocalhost password
需要注意的是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 readPropertyFileDB.properties::db.host = localhostDB.properties::db.user = userDB.properties::db.pwd = passwordDB.properties::XYZ = nullDB.xml::db.host = localhostDB.xml::db.user = userDB.xml::db.pwd = passwordDB.xml::XYZ = null End of readPropertyFile Start of readAllKeysDB.properties:: Key=db.user::value=userDB.properties:: Key=db.host::value=localhostDB.properties:: Key=db.pwd::value=passwordDB.xml:: Key=db.user::value=userDB.xml:: Key=db.host::value=localhostDB.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:31) at com.journaldev.util.PropertyFilesUtil.main(PropertyFilesUtil.java:21)
以上就是XML作为属性文件的使用的详细内容了,看完之后是否有所收获呢?如果想了解更多相关内容,欢迎来创新互联行业资讯!