书单推荐:成为Java顶级程序员架构师 ,这20来本(高薪)必看点击获取
在java中有一种属性文件(资源文件)的定义:*.properties文件,在这种文件里面其内容的保存形式为 “key = value” ,通过ResourceBundle类读取的时候只能读取内容,要想编辑其内容则需要通过Properties类来完成,这个类是专门做属性处理的。
java如何遍历properties文件?
public class PropertiesUtil {
public static Map getFileIO(String fileName){
Properties prop = new Properties();
Map propMap=new HashMap();
InputStream in = PropertiesUtil.class.getResourceAsStream(fileName);
try {
prop.load(in);
Setkeyset = 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;
}
public static void main(String[] args) {
getFileIO("/loginurl.properties");
}
}转载请注明:谷谷点程序 » java获取遍历properties配置文件