gpt4 book ai didi

java - 如何在运行时从 jar 中复制文件?

转载 作者:行者123 更新时间:2023-11-30 02:23:32 26 4
gpt4 key购买 nike

我想将一个文件从项目中的 src 文件复制到我的目录,但当我导出到可运行的 jar 时它不起作用。

      public static void main(String args[]) throws IOException{
FileCopyController fpc = new FileCopyController();
File fileSrc = new File("src/java.exe");
File fileDest = new File("C:/Directory1/java.exe");
fpc.copyFileUsingChannel(fileSrc, fileDest);
}

public void copyFileUsingChannel(File source, File dest) throws IOException {
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(source);
os = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} finally {
is.close();
os.close();
}

最佳答案

尝试这样的事情:

public static void main(String args[]) throws IOException
{
final InputStrean src = getClass().getResourceAsStream("/java.exe");
final Path dest = new File("C:/Directory1/java.exe").toPath();
Files.copy(src, dest, StandardCopyOption.REPLACE_EXISTING);
}

关于java - 如何在运行时从 jar 中复制文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46232358/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com