获得项目classpath下面的资源文件

//加载资源配置文件(System.properties位于src下面,发布后会在classpath下面)
//从classpath下面加载资源文件
private static Properties props;
static
{
props=new Properties();
try {

InputStream in = TradeExport.class.getClassLoader().getResourceAsStream("System.properties");
//据说下面的方法更好
Thread.currentThread().getContextClassLoader().getResourceAsStream("System.properties");
props.load(in);
} catch (Exception e) {
e.printStackTrace();
};
}

注:
类名.class.getClassLoader().getResource("").getPath(); //获得classpath路径,此方法getResource会对地址进行"utf-8"进行编码,因此如果路径中有空格会出现"20%"的字样,需要使用java.net.URLDecoder.decode("path","utf-8")来还原路径!

你可能感兴趣的:(Java,.net,java)