您的位置:首页 > 编程语言 > Java开发

java 获取properties配置文件例子

2012-08-29 14:53 441 查看
import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.Properties;

public class Test {

public static void main(String[] arg){

Properties pro=getProperties();

String serverUrl= pro.getProperty("serverUrl");

System.out.println(serverUrl);

}

//获取属性文件

private static Properties getProperties(){

String tomcatPath = Test.class.getClassLoader().getResource("").toString();

System.out.println(tomcatPath);

if(tomcatPath!=null&&tomcatPath.length()>0){

int s = tomcatPath.lastIndexOf("classes");

tomcatPath = tomcatPath.substring(0,s );

int pos=tomcatPath.indexOf("file:");

if(pos>-1) tomcatPath=tomcatPath.substring(pos+5);

}

String sourceFileName=tomcatPath+"config/systemConfig.properties";//获取配置文件的地址

try{

sourceFileName=java.net.URLDecoder.decode(sourceFileName,"utf-8");

}catch(Exception e){throw new RuntimeException(e);}

FileInputStream in;

Properties prop = new Properties();

try {

in = new FileInputStream(sourceFileName);

prop.load(in);

} catch (FileNotFoundException e) {

e.printStackTrace();

}catch (IOException e) {

e.printStackTrace();

}

return prop;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: