主要是通过getResourceAsStream方法来实现相对路径的读取,相对路径的意思就是以项目所在路径为基准,让程序知道其在项目的哪个路径下面。
public class ReadProperties {
private static ReadProperties instance;
private static Properties prop;
private ReadProperties (){}
/*public static ReadProperties getInstance() {
if (instance == null) {
instance = new ReadProperties();
}
return instance;
} */
public static Properties readProp(){
if(prop==null){
prop=new Properties();
try{
prop.load(ReadProperties.class.getResourceAsStream("/hbase.properties"));
Enumeration<String> en = (Enumeration<String>)prop.propertyNames();
while(en.hasMoreElements()){
String confkey = en.nextElement();
String value = prop.getProperty(confkey);
System.out.println("key:"+confkey+","+"value:"+value);
}
}catch(Exception e){
e.printStackTrace();
}
}
return prop;
}
}
hbase.properties的内容:
tableName =helloHbase
public static void main(String[] agrs)
{
String tablename =ReadProperties.readProp().getProperty("tableName");
System.out.println(tablename);
}