JAVA 讀取properties文件

java的properties文件需要放到classpath或者classpath子目錄下面,這樣程序才能讀取到,有關classpath實際上就是java類或者庫的存放 路徑



package com.props.lfn;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public final class PropertyReader
{
public static final String DEFAULT_PROPERTYFILE = "/WEB-INF/jspwiki.properties";
static Properties props= new Properties();
static String value = "";

/**
* Private constructor to prevent instantiation.
*/
private PropertyReader()
{}
public static final String loadRedisProperties(){
InputStream propertyStream = null;
propertyStream = PropertyReader.class.getResourceAsStream(DEFAULT_PROPERTYFILE);
try {
props.load(propertyStream);
System.out.println("----"+props.getProperty("xxx.server.config"));
if(props == null){
System.out.println("propes is null");
}
value = props.getProperty("redis.server.config");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return value;
}

public static void main(String[] args){
// loadRedisProperties();
System.out.println("--ddd--"+loadRedisProperties());
}
}


//獲取項目絕對路徑方法

public static String getAbsolutePath() throws IOException{
File f = new File(ProjectClassPath.class.getResource("/").getPath());
System.out.println(f);
return f.toString();
}






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