两种办法 第一: File f = new File(this.getClass().getResource("/").getPath());
f = new File(f.getPath() + "/conf/config.properties");
注:f.getPath()即为当class所在的绝对路径。如:c:\javasrc\web-inf\classes
然后,对文件对象进行处理,就能把配置信息读取出来了,但是加入如上class被打包成jar文件,那么,在程序执行到这里时,就会无法找到配置文件,那么该如何处理呢?
处理方法如下:
String s_config="conf/config.properties";
File file= new File(String.valueOf(ClassLoader.getSystemResource(config)));
String filepaths= file.getPath(); 第二:用JarFile JarFile jarFile = new JarFile("thefile.jar"); 这个是专门用来读jar文件的
如果觉得《Java eclipse导出的jar怎样读写里面的properties文件》对你有帮助,请点赞、收藏,并留下你的观点哦!