在java项目开发过程当中,使用properties文件做为配置基本上是必不可少的,不少如系统配置信息,文件上传配置信息等等都是以这种方式进行保存。
同时学会操做properties文件也是java基础。 java
/**
* @Copyright @ 2012
* All right reserved
* @version 建立时间:Created on Oct 31, 2012
* @author 做者:Create by www.360buyli.com
* @Email : 360buyli@gmail.com
* @description java如何读取和遍历properties文件
*/ url
public class PropertiesUtil {
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;
}
public static void main(String[] args) {
getFileIO("/loginurl.properties");
}
} .net