首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

怎么读取*properties文件

2013-04-26 
如何读取*.properties文件?1.方法一:ResourceBundle.getBundle(properties文件名的前半部分)?2.方法二:

如何读取*.properties文件

?

1.方法一:ResourceBundle.getBundle("properties文件名的前半部分");

?

2.方法二:ClassRoader.getResourceAsStream(" properties全名");

?

?

import java.io.InputStream;import java.util.Enumeration;import java.util.List;import java.util.Properties;import java.util.ResourceBundle;import org.junit.Test;/** * 获取*.properties配置文件中的内容  *  */public class ReadProperties {// 方法一@Testpublic void One() {// 获得资源包ResourceBundle bundle = ResourceBundle.getBundle("test");// 通过资源包拿到所有的名称Enumeration<String> allName = bundle.getKeys();// 遍历while (allName.hasMoreElements()) {// 获取每一个名称String name = (String) allName.nextElement();// 利用已得到的名称通过资源包获得相应的值String value = bundle.getString(name);System.out.println(name + "=" + value);}}// 方法二@Testpublic void Two() throws Exception {// 获得类加载器,然后把文件作为一个流获取InputStream in = ReadProperties.class.getClassLoader().getResourceAsStream("test.properties");// 创建Properties实例Properties prop = new Properties();// 将Properties和流关联prop.load(in);// 获取所有的名称Enumeration<?> allName = prop.propertyNames();// 遍历while (allName.hasMoreElements()) {// 获得每一个名称String name = (String) allName.nextElement();// 利用已得到的名称通过Properties对象获得相应的值String value = (String) prop.get(name);System.out.println(name + "=" + value);}// 关闭资源in.close();}}

?

热点排行