JAVA讀取properties文件

 
JAVA讀取properties文件
 
test.properties
keykey=Oh.I get the value!
 
測試類SuperPath.java
package com.test.superpath;

import java.io.InputStream;
import java.util.Properties;

/**    
* @author 李晨        
* @version 創建時間:Jul 17, 2009 3:11:40 PM    
*/
public class SuperPath {

/**
* 從配置文件取值
* @param fileName 配置文件名
* @return 從指定key得到的value
*/
   public String getPara(String fileName) {

    Properties prop = new Properties();
     try {
       // ClassLoader cl = this.getClass().getClassLoader();
       // InputStream is = cl.getResourceAsStream(fileName);
      InputStream is = this.getClass().getResourceAsStream(fileName);
      prop.load(is);
       if (is != null)
        is.close();
    } catch (Exception e) {
      System.out.println(e + "file " + fileName + " not found");
    }
     /**
     * 指定哪個key
     */
     return prop.getProperty( "keykey");
  }

   public static void main(String[] args) {
    SuperPath sp= new SuperPath();
    System.out.println(sp.getPara( "test.properties"));
  }

}
 
輸出結果:
Oh.I get the value!
 

本文出自 “wIsper 把技術做成藝術” 博客,請務必保留此出處http://lichen.blog.51cto.com/697816/189964

轉載於:https://my.oschina.net/lichen/blog/264911

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