【问题标题】:FileNotFoundException from new FileOutputStream来自新 FileOutputStream 的 FileNotFoundException
【发布时间】: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


【解决方案1】:

谁能给我一个从 jar 文件中获取文件的方法?

我已经编写了一些实用程序类来处理基于 NIO.2 文件 API 的 JAR/ZIP(该库是开源的):

马文:

<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

【讨论】:

    猜你喜欢
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 2013-12-08
    • 2019-09-17
    • 2013-10-24
    相关资源
    最近更新 更多