简体   繁体   English

从.jar文件复制文件夹

[英]Copying a folder from a .jar file

I created a class called FileCopyFromJAR that has a static method called copy. 我创建了一个名为FileCopyFromJAR的类,该类具有一个称为copy的静态方法。 This allows me to copy files to outside of the JAR by using getResourceAsStream: 这使我可以使用getResourceAsStream将文件复制到JAR外部:

 public static void copy(String source,String dest) throws IOException{
    try{
        File sourceFile = new File(source);
        File destFile = new File(dest);
        InputStream in = FileCopyFromJAR.class.getResourceAsStream(source);
        OutputStream out = new FileOutputStream(destFile);
        int bufferSize = 1024;
        byte[] buf = new byte[bufferSize];
        int len;
        while ((len = in.read(buf)) > 0){
            out.write(buf,0,len);
        }
        in.close();
        out.close();
    }
    catch(IOException e){
        throw e;
    }
}

Inside my code, I will call this by doing something like (this assumes test.txt is in the root of my .jar file): 在我的代码内,我将通过执行类似的操作来调用此命令(这假设test.txt位于我的.jar文件的根目录中):

FileCopyFromJAR.copy("/test.txt","c:\\test.txt");

However, I get a FileNotFoundException if I specify a folder or a file inside a folder. 但是,如果指定文件夹或文件夹中的文件,则会收到FileNotFoundException。 Both of these return an error: 这两个都返回错误:

FileCopyFromJAR.copy("folder\test.txt","c:\\test.txt");
FileCopyFromJAR.copy("folder", "c:\\folder");

I've also tried using various combinations like /folder\\test.txt, etc. but nothing seems to work. 我也尝试过使用各种组合,例如/folder\\test.txt等,但似乎无济于事。 Is there a way to make this work or do I have to use a different method? 有没有办法使这项工作有效,还是必须使用其他方法?

I figured it out! 我想到了! I broke out my "Core Java Volume 1" 9th edition book and it says "Note that you must always use / separator, regardless of the directory separator on the system that actually stores the resource files." 我出版了《 Core Java Volume 1》第9版,上面写着“请注意,无论实际存储资源文件的系统上的目录分隔符是什么,都必须始终使用/分隔符。” (Horstmann, page 571, chapter 10.1 JAR Files). (Horstmann,第571页,第10.1章JAR文件)。

So this works: 所以这有效:

FileCopyFromJAR.copy("/folder/test.txt", "c:\\test12.txt");

Hope this helps anyone out there! 希望这可以帮助任何人! It was driving me crazy! 这让我发疯了!

According to the class.getResourceAsStream documentation : "The rules for searching resources associated with a given class are implemented by the defining class loader of the class. " 根据class.getResourceAsStream 文档 :“用于搜索与给定类关联的资源的规则由该类的定义类加载器实现。”

And then for loader class we have: 然后对于加载程序类,我们有:

Any class name provided as a String parameter to methods in ClassLoader must be a binary name as defined by the Java Language Specification . 在ClassLoader中作为String参数提供给方法的任何类名称都必须是Java语言规范定义的二进制名称。

Examples of valid class names include: 有效类名的示例包括:

"java.lang.String"
"javax.swing.JSpinner$DefaultEditor"
"java.security.KeyStore$Builder$FileBuilder$1"
"java.net.URLClassLoader$3$1"

Before delegation, an absolute resource name is constructed from the given resource name using this algorithm: 在委派之前,使用以下算法从给定资源名称构造绝对资源名称:

  • If the name begins with a '/' ('\/'), then the absolute name of the resource is the portion of the name following the '/'. 如果名称以'/'('\\ u002f')开头,则资源的绝对名称是名称中'/'之后的部分。
  • Otherwise, the absolute name is of the following form: 否则,绝对名称的格式如下:
    modified_package_name/name modified_pa​​ckage_name /名称

Where the modified_package_name is the package name of this object with '/' substituted for '.' 其中Modifyed_pa​​ckage_name是此对象的软件包名称,用“ /”代替“。”。 ('\.'). ( '\\ u002e')。

So you should use : 所以你应该使用:

source = "Package_name" + ".folder.test" //remove the ".txt" from the file
FileCopyFromJAR.class.getResourceAsStream(source);

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

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