最佳答案
我已经将我的应用程序部署到 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
我遇到的错误是: URI is not hierarchical
.this 在 IDE 中运行时不会遇到的错误。
如果我在 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
//....
}