我正在尝试创建一个程序,它将获取jar文件中的所有文件,然后复制它们。 这是我用来复制文件的代码:
public static void copyFolder(File src, File dest)
throws IOException{
if(src.isDirectory()){
//if directory not exists, create it
if(!dest.exists()){
dest.mkdir();
System.out.println("Directory copied from "
+ src + " to " + dest);
}
//list all the directory contents
String files[] = src.list();
for (String file : files) {
//construct the src and dest file structure
File srcFile = new File(src, file);
File destFile = new File(dest, file);
//recursive copy
copyFolder(srcFile,destFile);
}
}else{
//if file, then copy it
//Use bytes stream to support all file types
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.close();
System.out.println("File copied from " + src + " to " + dest);
}
}
但是有一个错误 - 在OutputStream out = new FileOutputStream(dest);中java.io.FileNotFoundException :(拒绝访问)。现在,我不知道为什么或这是什么意思?我该如何解决?
另外,我完全不知道如何从jar文件中提取文件。我看过ZipFile课程,但我真的不知道如何使用它...所以这给我留下了3个问题: 1. 复制代码有什么问题? 2. 拒绝访问是什么意思? 3. 任何人都可以给我一个从jar文件中获取文件的方法吗?因为jar.listFiles()返回一个空列表。
提前致谢!
答案 0 :(得分:0)
有人能给我一个从jar文件中获取文件的方法吗?
我已经编写了一些实用程序类来处理基于NIO.2文件API的JAR / ZIP(该库是开源的):
的Maven:
<dependency>
<groupId>org.softsmithy.lib</groupId>
<artifactId>softsmithy-lib-core</artifactId>
<version>0.4</version>
</dependency>
教程: