天天看點

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!

本文轉自chainli 51CTO部落格,原文連結:http://blog.51cto.com/lichen/189964,如需轉載請自行聯系原作者