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;
}
}
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;
}
}
相关文章推荐
- Java配置类获取properties文件
- java获取properties配置文件值
- 获取java web项目里面所有的properties文件并组装在一起,使用方便,配置简单
- 【java】获取properties配置文件信息工具类
- java中经常用到的读写配置文件的信息properties属性的写入,读取例子
- java 获取properties配置文件属性值
- Java中如何获取spring中配置的properties属性文件内容
- java工具类获取properties文件的配置
- Java代码中获取配置文件(config.properties)中内容的两种方法
- java 获取properties配置文件内容
- Java中如何获取spring中配置的properties属性文件内容
- java 获取.properties配置文件方式(存放在src下面),获取部署项目的路劲
- Java 获取*.properties配置文件中的内容 ,常见的两种方法
- java获取当前路径及加载配置文件(报错:java.lang.NullPointerException java.util.Properties$LineReader.readLine(Prop)
- Java中如何获取spring中配置的properties属性文件内容
- java中经常用到的读写配置文件的信息properties属性的写入,读取例子
- Java中如何获取spring中配置的properties属性文件内容
- java获取web工程下文件的路径,如配置文件properties
- Java获取.properties配置文件某一项value根据key值
- Java中如何获取spring中配置的properties文件内容