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

Java遍历properties资料

2012-12-24 
Java遍历properties文件//1. new java.utils.propertiesProperties p new Properties()//2. 读取文件te

Java遍历properties文件

//1. new java.utils.properties

Properties p = new Properties();

//2. 读取文件test.properties

p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("test.properties"));

?

?

如果test文件里面内容是a=b /换行 c=d 这种格式

可以直接通过p.get("a")拿到值b

?

?

?

?

?

-----------------------------------遍历---------------------------------------

public static Map<String,String> getFileIO(String fileName){
Properties prop = new Properties();
Map<String,String> propMap=new HashMap<String, String>();
InputStream in = PropertiesUtil.class.getResourceAsStream(fileName);
try {
prop.load(in);
Set<Object> keyset = prop.keySet();
for (Object object : keyset) {
String propValue= prop.getProperty(object.toString()).toString();
propMap.put(object.toString(), prop.getProperty(object.toString()).toString());
System.out.println(object.toString()+" : "+propValue);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

热点排行