java如何讀取和遍歷properties文件

在java項目開發過程中,使用properties文件作爲配置基本上是必不可少的,很多如系統配置信息,文件上傳配置信息等等都是以這種方式進行保存。
同時學會操作properties文件也是java基礎。

 

以下爲java代碼:

/**
 * @Copyright @ 2012
 * All right reserved
 * @version 創建時間:Created on Oct 31, 2012
 * @author 作者:Create by www.360buyli.com
 * @Email: [email protected]
 * @description java如何讀取和遍歷properties文件
 */

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");
 }
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章