[英]Copy JAR file from inside resources to temp folder
我的 Java 項目中有一些 JAR 文件,我需要將它們復制到文件系統的臨時文件夾中。 但是,一旦復制,所有復制的文件都會損壞,即使它們的大小與原始文件不同(當使用 7Zip 打開 zip 時會出現未格式化的文件,而不是 jar 的文件夾結構)。 我的程序中的任何進程都不需要這些文件,比如編譯,只是為了復制到系統。
我已經嘗試過一些解決方案,例如this 、 this或使用 CommonsIO 庫,但復制的文件總是損壞。
編輯:我嘗試過的一些代碼是
InputStream source = getClass().getClassLoader().getResourceAsStream(originPath);
Files.copy(source, Paths.get(destPath), StandardCopyOption.REPLACE_EXISTING);
source.close();
byte[] source = IOUtils.toByteArray(getClass().getClassLoader().getResourceAsStream(originPath));
File directoryFile = new File(originPath);
if(!directoryFile.exists()) {
directoryFile.mkdirs();
}
try (FileOutputStream fileOutputStream = new FileOutputStream(destPath)){
fileOutputStream.write(source);
}
catch (Exception exception) {
throw new IOException(String.format("Failed to copy the file %s", sourceName));
}
我怎樣才能做到這一點?
你有沒有嘗試過:
Path source = Paths.get(...)
Path dest = Paths.get(...)
Files.move(source,dest);
另一種方式:
File source = new File("....");
File dest = new File("...");
file.renameTo(dest);
基本上,您需要在文件系統中而不是在現有應用程序的類路徑中處理文件,因此我認為使用getClass().getResourceAsStream()
沒有意義
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.