我正在尝试制作一个程序,该程序将获取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()返回一个空列表。
提前感谢!
有人能给我一个从jar文件中获取文件的方法吗?
我已经编写了一些实用程序类来使用基于NIO.2文件API的JAR/ZIP(库是开源的):
Maven:
<dependency>
<groupId>org.softsmithy.lib</groupId>
<artifactId>softsmithy-lib-core</artifactId>
<version>0.4</version>
</dependency>
教程:
http://softsmithy.sourceforge.net/lib/current/docs/tutorial/nio-file/index.html#ExtractJarResourceSample