相关疑难解决方法(0)

Java Jar文件:使用资源错误:URI不是分层的

我已将我的应用程序部署到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
Run Code Online (Sandbox Code Playgroud)

我遇到的错误是: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) { …
Run Code Online (Sandbox Code Playgroud)

java deployment resources executable-jar

54
推荐指数
3
解决办法
7万
查看次数

NetBeans Java项目文本文件路径

我有以下代码来读取文本文件.

public static void main(String[] args)
{
    try 
    {
    Scanner in = new Scanner(new FileReader("input.txt"));
    while(in.hasNext())
    {
        System.out.println(in.next());
    }
} 
catch (FileNotFoundException ex) 
{
    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
Run Code Online (Sandbox Code Playgroud)

我的项目结构设置如下:

build/ directory contains class
dist/  directory contains the jar file 
src/ directory contains source
input.txt the text file to read
Run Code Online (Sandbox Code Playgroud)

如果我把我的文本文件input.txt到一个名为目录test是相同的目录作为build,dist以及src什么应该进入的参数filereader,这样我仍然可以找到这个文件?

java netbeans path

10
推荐指数
2
解决办法
4万
查看次数

标签 统计

java ×2

deployment ×1

executable-jar ×1

netbeans ×1

path ×1

resources ×1