热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

javaurljar包_JavaJar文件:使用资源错误:URI不分层

我已将我的应用程序部署到jar文件。当我需要将数据从一个资源文件复制到jar文件外部时,请执行以下代码:URLresourceUrlgetClass()

我已将我的应用程序部署到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

//....

}



推荐阅读
author-avatar
林淑芬若纯嘉友
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有