我已将我的应用程序部署到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) 我有以下代码来读取文本文件.
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,这样我仍然可以找到这个文件?