Java,无法删除Windows上的文件



我的应用程序有一个简单的更新程序。在代码中,我正在下载新版本,删除旧版本并将新版本重命名为旧版本。

它在Linux上运行良好。但不适用于Windows。没有例外或其他东西。p.s. 远程播放器.jar它当前正在运行的应用程序。

更新:

不起作用 - 这意味着在 file.delete() 和 file.renameTo(... 文件仍然活着。我使用Sun Java 7。(因为我使用JavaFX)。

附言对不起我的英语。

public void checkUpdate(){
    new Thread(new Runnable() {
        @Override
        public void run() {
            System.err.println("Start of checking for update.");
            StringBuilder url = new StringBuilder();
            url.append(NetworkManager.SERVER_URL).append("/torock/getlastversionsize");
            File curJarFile = null;
            File newJarFile  = null;
            try {
                curJarFile = new File(new File(".").getCanonicalPath() + "/Player/RemotePlayer.jar");
                newJarFile = new File(new File(".").getCanonicalPath() + "/Player/RemotePlayerTemp.jar");
                if (newJarFile.exists()){
                    newJarFile.delete();
                }
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                System.err.println("Cannot find curr Jar file");
                return;
            }
            if (curJarFile.exists()){
                setAccesToFile(curJarFile);
                try {
                    String resp = NetworkManager.makeGetRequest(url.toString());
                    JSONObject jsresp = new JSONObject(resp);
                    if (jsresp.getString("st").equals("ok")){
                        if (jsresp.getInt("size") != curJarFile.length()){
                            System.out.println("New version available, downloading started.");
                            StringBuilder downloadURL = new StringBuilder();
                            downloadURL.append(NetworkManager.SERVER_URL).append("/torock/getlatestversion");
                            if (NetworkManager.downLoadFile(downloadURL.toString(), newJarFile)){
                                if (jsresp.getString("md5").equals(Tools.md5File(newJarFile))){
                                    setAccesToFile(newJarFile);
                                    System.err.println("Deleting old version. File = " + curJarFile.getCanonicalPath());
                                    boolean b = false;
                                    if (curJarFile.canWrite() && curJarFile.canRead()){
                                        curJarFile.delete();
                                    }else System.err.println("Cannot delete cur file, doesn't have permission");
                                    System.err.println("Installing new version. new File = " + newJarFile.getCanonicalPath());
                                    if (curJarFile.canWrite() && curJarFile.canRead()){
                                        newJarFile.renameTo(curJarFile);
                                        b = true;
                                    }else System.err.println("Cannot rename new file, doesn't have permission");
                                    System.err.println("last version has been installed. new File  = " + newJarFile.getCanonicalPath());
                                    if (b){
                                        Platform.runLater(new Runnable() {
                                            @Override
                                            public void run() {
                                                JOptionPane.showMessageDialog(null, String.format("Внимание, %s", "Установлена новая версия, перезапустите приложение" + "", "Внимание", JOptionPane.ERROR_MESSAGE));
                                            }
                                        });
                                    }
                                }else System.err.println("Downloading file failed, md5 doesn't match.");
                            }
                        } else System.err.println("You use latest version of application");
                    }
                }catch (Exception e){
                    e.printStackTrace();
                    System.err.println("Cannot check new version.");
                }
            }else {
                System.err.println("Current jar file not found");
            }
        }
    }).start();
}
private void setAccesToFile(File f){
    f.setReadable(true, false);
    f.setExecutable(true, false);
    f.setWritable(true, false);
}

我找到了这个问题的解决方案。在我的情况下发生了删除问题,因为-:

File f1=new File("temp.txt");
RandomAccessFile raf=new RandomAccessFile(f1,"rw");
f1.delete();//The file will not get deleted because raf is open on the file to be deleted

但是如果我在调用删除之前关闭随机访问文件,那么我就可以删除该文件。

File f1=new File("temp.txt");
RandomAccessFile raf=new RandomAccessFile(f1,"rw");
raf.close();
f1.delete();//Now the file will  get deleted

因此,我们必须在调用删除天气之前检查任何对象,例如FileInputStream,RandomAccessFile是否在该文件上打开。如果是,那么我们必须在对该文件调用 delete 之前关闭该对象。

Windows 锁定当前正在使用的文件。 您无法删除它们。 在 Windows 上,您无法删除应用程序当前正在使用的 JAR 文件。

由于您使用的是 Java 7,请尝试 java.nio.file.Files.delete(file.toPath()) ,如果删除失败,它将引发异常。

有几个原因:

  1. 您是否有权在窗口中编辑文件。

  2. 文件是否正在使用中。

  3. 道路是对还是错。

我不知道

你正在使用的Java版本。

我知道当 Java 是 sun 属性时,他们发布对象文件无法在 windows 平台上正确删除文件(抱歉,我找不到更多参考)。

您可以做的技巧是直接测试平台。当你在Linux上时,只需使用经典的File对象。

在窗口上启动命令系统,要求窗口删除所需的文件。

Runtime.getRuntime().exec(String command);
<</div> div class="one_answers">

我只想发表一个评论。我了解到,如果您以管理员身份运行 eclipse 程序,则可以从 eclipse 中删除 Java 中的文件。 即,当您右键单击 IDE 图标(Eclipse 或任何其他 IDE)并选择以管理员身份运行时,Windows 允许您删除该文件。

我希望这有所帮助。 它帮助了我。

亲切

费尔南多

最新更新