尝试移动文件以覆盖另一个文件时返回null

时间:2019-02-17 08:36:20

标签: java

我正在尝试将 myJar.jar 从更新文件夹 F:\ Test Server \ plugins \ update 移到 F:\ Test Server \ plugins

我相信我正在弄乱路径。 我尝试过:

new File(PluginManager.class.getProtectionDomain()
                     .getCodeSource()
                     .getLocation()
                     .getPath()); 
                     //F:\Test%20Server\plugins\myPlugin.jar (just a test)

new File("update" + File.separator).listFiles(); //this however just produces null

File file = new File(PluginManager.class.getProtectionDomain()
                     .getCodeSource()
                     .getLocation()
                     .getPath()); //this gets the path where current running jar is

File[] directories = new File(String.valueOf(file))
                       .listFiles(File::isDirectory);

if (directories != null) {
    for (File dir : directories)
        System.out.println(dir.toString());
    }
}
//this also produces null - I got this code from the internet

最后,我尝试了:

File theFile = new File("F:\\Test%20Server\\plugins\\update\\myJar.jar");
if (!(theFile.exists())) { //this is always the result
    System.out.println("not found myJar.jar");
} else {
    try {
        Files.move(Paths.get("/update/myJar.jar"), 
                   Paths.get("myJar.jar"), 
                   StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

我希望它实际上不会一直在说“未找到myJar.jar”,但可惜我认为我在做菜鸟错误。

1 个答案:

答案 0 :(得分:-1)

我想出了办法;

File a = new File("F:\\Test-Server\\plugins\\update\\myJar.jar");
    a.renameTo(new File("F:\\Test-Server\\plugins\\" + a.getName()));
    a.delete();

src