//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;
}
本文介绍了一种使用Java来加载和遍历属性文件的方法。通过实例演示了如何利用`Properties`类读取名为`test.properties`的文件,并通过遍历获取其键值对。此外还提供了一个静态方法`getFileIO`,该方法能够返回所有键值对组成的映射。
2万+

被折叠的 条评论
为什么被折叠?



