读取配置文件中数据

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

public class PropUtil {
	 /**
     * 读取properties文件返回Properties
     * @return 配置文件的Properties对象
     */
    public static Properties readConfig(String url){

        Properties p=new Properties();
        InputStream in =null;
        try {
           in= PropUtil.class.getResourceAsStream(url); //创建文件输入流
           p.load(in);                    //通过输入流加载properties配置文件到properties对象中
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            if(in!=null){
                try {
                    in.close();         //关闭输入流
                } catch (IOException e) {
                     e.printStackTrace();
                }
            }
        }
        return p;
    }
}

调用方法:
String pwd = PropUtil.readConfig("/server.properties").getProperty("password");
其中server.propertise在src目录下

猜你喜欢

转载自liugang-ok.iteye.com/blog/1888420