Java Jar 文件:使用资源错误:URI 不是分层的
2022-08-31 13:09:21
我已将我的应用部署到 jar 文件。当我需要将数据从一个资源文件复制到jar文件外部时,我会执行以下代码:
URL resourceUrl = getClass().getResource("/resource/data.sav");
File src = new File(resourceUrl.toURI()); //ERROR HERE
File dst = new File(CurrentPath()+"data.sav"); //CurrentPath: path of jar file don't include jar file name
FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(dst);
// some excute code here
我遇到的错误是:。在IDE中运行时,我遇到此错误。URI is not hierarchical
如果我将上面的代码作为StackOverFlow上其他帖子的一些帮助:
InputStream in = Model.class.getClassLoader().getResourceAsStream("/resource/data.sav");
File dst = new File(CurrentPath() + "data.sav");
FileOutputStream out = new FileOutputStream(dst);
//....
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) { //NULL POINTER EXCEPTION
//....
}