简体   繁体   English

来自新 FileOutputStream 的 FileNotFoundException

[英]FileNotFoundException from new FileOutputStream

I am trying to make a program that will get all files within a jar file, and then copy them.我正在尝试制作一个程序,它将获取 jar 文件中的所有文件,然后复制它们。 This is the code I am using to copying files:这是我用来复制文件的代码:

 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);
    }
}

But there is an error - java.io.FileNotFoundException: (Access is denied) in OutputStream out = new FileOutputStream(dest);.但是有一个错误 - java.io.FileNotFoundException: (Access is denied) in OutputStream out = new FileOutputStream(dest);。 Now, I have no idea why or what does that really mean?现在,我不知道为什么或这到底意味着什么? How can I fix it?我该如何解决?

Plus, I have absolutely no idea how to extract files from a jar file.另外,我完全不知道如何从 jar 文件中提取文件。 I have seen the ZipFile class but I don't really know how to use it... So that leaves me with 3 questions: 1. Whats wrong with the copying code?我已经看过 ZipFile 类,但我真的不知道如何使用它......所以这给我留下了 3 个问题: 1.复制代码有什么问题? 2. What does Access is denied mean? 2.拒绝访问是什么意思? 3. Can anyone give me a method for getting files from a jar file? 3.谁能给我一个从jar文件中获取文件的方法? Because jar.listFiles() returns an empty list.因为 jar.listFiles() 返回一个空列表。

Thanks in advance!提前致谢!

Can anyone give me a method for getting files from a jar file?谁能给我一种从 jar 文件中获取文件的方法?

I've written some utility classes to work with JARs/ ZIPs based on the NIO.2 File API (the library is Open Source):我已经编写了一些实用程序类来处理基于 NIO.2 文件 API(该库是开源的)的 JAR/ZIP:

Maven:马文:

<dependency>  
    <groupId>org.softsmithy.lib</groupId>  
    <artifactId>softsmithy-lib-core</artifactId>  
    <version>0.4</version>  
</dependency> 

Tutorial:教程:

http://softsmithy.sourceforge.net/lib/current/docs/tutorial/nio-file/index.html#ExtractJarResourceSample http://softsmithy.sourceforge.net/lib/current/docs/tutorial/nio-file/index.html#ExtractJarResourceSample

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM