java properties 文件_Java Properties文件

Java属性(Properties)文件用于存储键值对配置。java.util.Properties类用于处理java中的属性文件。

在java中,属性文件可以是具有键值对的普通属性文件,也可以是XML文件。

在这个java属性文件示例中,演示示如何以两种格式编写属性(Properties)文件,然后从两个配置文件中读取属性。

我们还将演示如何从类路径加载属性文件以及如何从属性文件中读取所有键。

Java属性文件示例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 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");

}

/**

* 此方法将属性文件写入属性文件中

* 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");

}

}

当运行上面的java属性文件示例程序时,writePropertyFile()方法将以两种格式写入属性文件,它将存储在项目根目录中。

以下是从writePropertyFile方法创建的属性文件。

文件:DB.properties -

#DB Config file

#Fri Nov 16 11:16:37 PST 2012

db.user=user

db.host=localhost

db.pwd=password12345

文件:DB.xml

DB Config XML file

user

localhost

password12345

注意属性文件中的注释,它是生成的,因为在编写文件时有注释内容。如果将注释作为null传递,则属性文件中将不会有注释。

以下是上述java属性文件程序的输出:

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 = password12345

DB.properties::XYZ = null

DB.xml::db.host = localhost

DB.xml::db.user = user

DB.xml::db.pwd = password12345

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=password12345

DB.xml:: Key=db.user::value=user

DB.xml:: Key=db.host::value=localhost

DB.xml:: Key=db.pwd::value=password12345

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.yiibai.util.PropertyFilesUtil.readPropertyFileFromClasspath(PropertyFilesUtil.java:31)

at com.yiibai.util.PropertyFilesUtil.main(PropertyFilesUtil.java:21)

因此,当只提供文件名时,它会在项目根目录中查找文件,该文件存储属性文件的位置。但是当我们尝试从类路径加载属性文件时,它会抛出NullPointerException,因为它尝试从Classpath加载文件,该类是项目的src目录。

因此,如果在类src目录中复制属性文件,它就可以加载它们并且工作正常。

在这种情况下,readPropertyFileFromClasspath方法的输出是:

DB.properties loaded from Classpath::db.host = localhost

DB.properties loaded from Classpath::db.user = user

DB.properties loaded from Classpath::db.pwd = password12345

DB.properties loaded from Classpath::XYZ = null

另外,请注意,当使用相同的Properties对象加载另一个属性文件时,应该使用clear()方法清除其内容。如果传递属性对象中没有任何键,则返回null。

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值