从标题中可以看到,我试图将导入到src中的各种文件复制到一个临时目录中。在这种情况下,在C驱动器中创建了一个名为"Dank“的文件夹。我想做的就是将这些内部文件(http://prntscr.com/d39rmt)复制到临时文件夹中。不要介意坏身体,这只是程序其余部分的片段,与此部分无关。谢谢。public class sstool extends FileNotFoundException { @SuppressWarnings({ "static-access", "unused" }) public static void main(String[] args) throws IOException { Path newDirectoryPath = Paths.get("C:\\Dank"); if (!Files.exists(newDirectoryPath)) { try { Files.createDirectory(newDirectoryPath); } catch (IOException e) { System.err.println(e); } }
发布于 2016-11-05 13:57:37
@Rogue离得很近。嵌入在应用程序中的文件通常使用Class.getResource或Class.getResourceAsStream读取。
绝对应该使用Files.copy,而不是采用两个路径参数的方法:
String[] files = {
"cheatsmasher.exe",
"Everything.ini",
"jdgui.jar",
"launcher.jar - Shortcut.lnk",
"LAV.cfg",
"LAV.exe",
"logs - Shortcut.lnk",
"pexplorer32.exe",
"pexplorer64.exe",
"Prefech.lnk",
"schem.exe",
"SE32.exe",
"SE64.exe",
"skypefiles.lnk",
"smash.dll",
"synpase.lnk",
"TM.lnk",
"TS3Client.lnk",
"USB32.cfg",
"USB32.exe",
"USB64.cfg",
"USB64.exe",
"Vape.dll",
"vapev.jar",
"versions - Shortcut.lnk",
};
Path destination = Paths.get("C:\\Dank");
Files.createDirectories(destination);
for (String file : files) {
try (InputStream stream =
SSTool.class.getResourceAsStream("/tools/" + file)) {
Files.copy(stream, destination.resolve(file));
}
}堆栈溢出的一个常见问题是您是否可以只列出目录的内容。答案是:--不,你不能。-- .jar文件只是一个扩展名不同的压缩文件,而一个zip文件中的目录根本不是一个目录,它只是另一个zip条目。(理论上,您可以迭代所有条目,查找具有特定前缀的条目,甚至可以尝试使用Java的zip文件系统,但这些解决方案都会遇到问题。)
https://stackoverflow.com/questions/40434533
复制相似问题