Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在jar外部复制二进制文件_Java_Jar_Exe_Java.util.scanner_Getresource - Fatal编程技术网

Java 在jar外部复制二进制文件

Java 在jar外部复制二进制文件,java,jar,exe,java.util.scanner,getresource,Java,Jar,Exe,Java.util.scanner,Getresource,我在jar文件中打包了一个exe,我正试图将它复制到一个临时位置,以便可以使用Desktop.browse()运行它,为此,我使用class.getResourceAsStream设置了一个带有输入流构造函数的扫描仪,然后使用printwriter将所有内容写入一个文件。出现的问题表明exe无效。我认为这是由于一些二进制数据丢失。如果有人能帮忙,请发表评论 Scanner sc = new Scanner(ClassBuilder.class.getResourceAsStream("j

我在jar文件中打包了一个exe,我正试图将它复制到一个临时位置,以便可以使用
Desktop.browse()
运行它,为此,我使用
class.getResourceAsStream
设置了一个带有输入流构造函数的扫描仪,然后使用
printwriter
将所有内容写入一个文件。出现的问题表明exe无效。我认为这是由于一些二进制数据丢失。如果有人能帮忙,请发表评论

    Scanner sc = new Scanner(ClassBuilder.class.getResourceAsStream("jd-gui.exe"));
    File copy = new File("C://users//Owner//Desktop//java//jd-gui.exe");
    copy.createNewFile();
    PrintWriter writer = new PrintWriter(copy);

    while(sc.hasNextLine())
        writer.println(sc.nextLine());

    writer.flush();
    writer.close();
    sc.close();

    Desktop.getDesktop().browse(copy.toURI()); 

如前所述,将流用于二进制数据。Commons io使复制流变得容易。比如:

InputStream in = getClass().getResourceAsStream("jd-gui.exe");
OutputStream out = new FileOutputStream("jd-gui.exe");
IOUtils.copy(in, out);

请出示密码。Make是短而甜的,只有片段才重要:扫描仪用于读取文本。PrintWriter用于编写文本。exe文件是二进制文件。使用流。您需要将.exe文件复制到新位置?getResourceAsStream()返回InputStream。从该流中读取字节,直到到达末尾,然后将其写入FileOutputStream。