【发布时间】:2014-08-16 16:00:01
【问题描述】:
我想将文件复制到另一个目录。我知道这已被问过数百万次,我阅读了大量关于此的答案,但我似乎无法让它工作。这是我目前使用的代码:
copyFile(new File(getClass().getResource("/jars/TurnOffClient.jar").toString()),
new File("C:\\Documents and Settings\\All Users\\Start Menu\\Programs\\Startup\\karioc.jar"));
这就是方法:
public static void copyFile(File sourceFile, File destFile) throws IOException {
if(!destFile.exists()) {
destFile.createNewFile();
}
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
destination.transferFrom(source, 0, source.size());
}
finally {
if(source != null) {
source.close();
}
if(destination != null) {
destination.close();
}
}
}
这是我的目录: Directories http://imageshack.com/a/img820/6418/5g3m.png
/////////////////////////////////////// //////////////////////////////////// 这是我得到的例外:
【问题讨论】:
-
你认为
getClass().getResource("/jars/TurnOffClient.jar")做了什么,为什么? -
这里的问题是独一无二的,因为他试图从 JAR 中复制文件。
-
getClass 获取类在机器中的路径?