我在jar里读取.properties文件时,总是找不到文件路径,后来用ResourceBundle读取.properties文件即可避免路径问题,代码如下:
//process为文件名,切记不要加 .properties, URL是文件里的键名
ResourceBundle bundle = ResourceBundle.getBundle("com.ihandy.smsoc.app.process");
String url = bundle.getString("URL");
ResourceBundle rs = ResourceBundle.getBundle("org.hello");//这里说明,hello文件就是在于classpath底下的,org包底下有这个文件hello.properties~
示例
props = new Properties();
props.load(new FileInputStream("src/conf/msf.properties"));//在工程底下src 目录的conf目录下
System.out.println(System.getProperty("user.dir")); //这个是去工程的绝对路径的
System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));
//这个是去当前classpath的uri的!
new Properties().load(new FileInputStream("test.properties"));//这里说明test文件在工程的跟目录下!
new Properties().load(new FileInputStream("test/test.properties"));//说明,在工程底下的test的文件夹底下的文件test.properties!