当前位置: 首页 > 知识库问答 >
问题:

如何将jar内的文件复制到jar外?

王才英
2023-03-14

我想从jar复制一个文件。我要复制的文件将被复制到工作目录之外。我做了一些测试,我尝试的所有方法最终都是0字节的文件。

编辑:我希望通过程序复制文件,而不是手动复制。

共有3个答案

范峰
2023-03-14

使用Java 7执行此操作的更快方法,以及获取当前目录的代码:

   /**
     * Copy a file from source to destination.
     *
     * @param source
     *        the source
     * @param destination
     *        the destination
     * @return True if succeeded , False if not
     */
    public static boolean copy(InputStream source , String destination) {
        boolean succeess = true;

        System.out.println("Copying ->" + source + "\n\tto ->" + destination);

        try {
            Files.copy(source, Paths.get(destination), StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException ex) {
            logger.log(Level.WARNING, "", ex);
            succeess = false;
        }

        return succeess;

    }

测试它(icon.png是应用程序包映像中的一个映像):

copy(getClass().getResourceAsStream("/image/icon.png"),getBasePathForClass(Main.class)+"icon.png");

关于代码行(getBasePathForClass(Main.class)):-

楚钊
2023-03-14

鉴于你对0字节文件的评论,我不得不假设你试图以编程方式做到这一点,并且,鉴于你的标签,你正在Java做到这一点。如果这是真的,那么只需使用Class.getResources()获取指向JAR中文件的URL,然后使用Apache Commons IO FileUtils.copyURLToFile()将其复制到文件系统。例如:

URL inputUrl = getClass().getResource("/absolute/path/of/source/in/jar/file");
File dest = new File("/path/to/destination/file");
FileUtils.copyURLToFile(inputUrl, dest);

最有可能的是,无论您现在有什么代码,问题都是您(正确地)使用缓冲输出流写入文件,但(错误地)未能关闭它。

哦,你应该编辑你的问题来明确你想怎么做(以编程方式,而不是语言,…)

阎兴为
2023-03-14

首先,我想说之前发布的一些答案是完全正确的,但是我想给出我的,因为有时候我们不能在GPL下使用开源库,或者因为我们懒得下载jar XD或者你的原因是什么这里有一个独立的解决方案。

下面的函数用于复制Jar文件旁边的资源:

  /**
     * Export a resource embedded into a Jar file to the local file path.
     *
     * @param resourceName ie.: "/SmartLibrary.dll"
     * @return The path to the exported resource
     * @throws Exception
     */
    static public String ExportResource(String resourceName) throws Exception {
        InputStream stream = null;
        OutputStream resStreamOut = null;
        String jarFolder;
        try {
            stream = ExecutingClass.class.getResourceAsStream(resourceName);//note that each / is a directory down in the "jar tree" been the jar the root of the tree
            if(stream == null) {
                throw new Exception("Cannot get resource \"" + resourceName + "\" from Jar file.");
            }

            int readBytes;
            byte[] buffer = new byte[4096];
            jarFolder = new File(ExecutingClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParentFile().getPath().replace('\\', '/');
            resStreamOut = new FileOutputStream(jarFolder + resourceName);
            while ((readBytes = stream.read(buffer)) > 0) {
                resStreamOut.write(buffer, 0, readBytes);
            }
        } catch (Exception ex) {
            throw ex;
        } finally {
            stream.close();
            resStreamOut.close();
        }

        return jarFolder + resourceName;
    }

只需将ExecutingClass更改为您的类的名称,并像这样调用它:

String fullPath = ExportResource("/myresource.ext");

正如GOXR3PLUS所回答和安迪·托马斯所指出的,你可以通过以下方式实现这一点:

Files.copy( InputStream in, Path target, CopyOption... options)

有关更多详细信息,请参阅GOXR3PLUS答案

 类似资料:
  • 问题内容: 我想从罐子中复制文件。我要复制的文件将被复制到工作目录之外。我已经做过一些测试,所有尝试使用的方法都以0字节文件结尾。 编辑 :我希望通过程序而不是手动完成文件的复制。 问题答案: 首先,我想说的是,之前发布的一些答案是完全正确的,但是我想告诉我,因为有时我们不能在GPL下使用开放源代码库,或者因为我们懒得下载jarXD或其他文件无论您的理由在这里,还是一个独立的解决方案。 下面的函数

  • 问题内容: 假设我的jar包“ com.test.io”中有一个名为test.txt的文件。 我将如何编写一个类来检索此文本文件,然后将内容复制到文件系统上的新文件中? 问题答案: 假设jar在您的类路径中:

  • 我有一个META-INF文件夹,其中有persitence.xml和orm.xml。当我用下面的gradle构建一个jar文件时,这些文件不会包含在jar中。如何将这些文件复制到jar中?

  • 我花了好几个小时寻找答案,但我还是想不出答案,我正试图将包含我正在处理的游戏的所有图像和数据文件的资源文件夹从运行的jar中复制到e:/program files/mtd中/当我从eclipse中运行它时,它工作得很好,但当我导出jar并尝试它时,我得到了NoSuchFileException

  • 问题内容: 我想将先前从其他文件(已经完成)中提取的一系列文件添加到jar中。这些文件将覆盖JAR中的文件。最有效的方法是什么?我需要它快。谢谢! 问题答案: 请记住,JAR文件是ZIP文件。 只需使用一些ZIP库。