【发布时间】:2014-04-17 11:53:36
【问题描述】:
我正在尝试制作一个程序,它将获取 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);
}
}
但是有一个错误 - java.io.FileNotFoundException: (Access is denied) in OutputStream out = new FileOutputStream(dest);。现在,我不知道为什么或这真正意味着什么?我该如何解决?
另外,我完全不知道如何从 jar 文件中提取文件。我看过 ZipFile 类,但我真的不知道如何使用它......所以这给我留下了 3 个问题: 1. 复制代码有什么问题? 2. 访问被拒绝是什么意思? 3. 谁能给我一个从 jar 文件中获取文件的方法? 因为 jar.listFiles() 返回一个空列表。
提前致谢!
【问题讨论】:
-
请粘贴堆栈跟踪。
-
如果您使用的是 java 版本 7,它内置了文件处理(Files.copy(src,dest,options)。要分析拒绝访问,我们将需要更多信息,如 Shoaib 所要求的。这也可能是一个重复的问题:stackoverflow.com/questions/1529611/…
标签: java jar outputstream access-denied