我的 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);
关于java - 将二进制文件复制到 jar 之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15558626/