我已将我的应用程序部署到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。在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
//....
}
开满天机
相关分类