java - File.delete() 方法不起作用..你能帮我吗?

标签 java java-io

我想制作这个密码更改器,首先创建一个新文件,然后将原始文件中的数据放入 string[] 数组中,并更改数组中的密码。然后我想删除原始文件(这不起作用),最后将数据放入新文件中并将其重命名为原始文件的名称。但它不会删除原来的,你知道我做错了什么吗?

 public static void wijzigenWachtwoordMainAccount(String pad, Integer nr)  throws IOException{
        wijzigdeel2(wijzigendeel1(pad, nr), pad, nr);
    }

    public static String[] wijzigendeel1(String pad, Integer nr) throws IOException{
        Scanner s = new Scanner(System.in);
        String naam = list.get(nr).name;
        String geb = list.get(nr).Gnaam;
        String[] geg; geg = new String[20];
        System.out.println(naam+", voer hier het nieuwe wachtwoord in:\n");
        String newww0 = s.nextLine();
        System.out.println("Voer het nieuwe wachtwoord opnieuw in:");
        String newww1 = s.nextLine();

        if (newww0.equals(newww1)) {
            File old = new File(pad + "\\Account\\" + geb + "\\" + geb + ".txt");
            File nieuw = new File(pad + "\\Account\\" + geb + "\\" + naam + ".txt");
            if (nieuw.createNewFile()) {
                System.out.println("Succes");
                String info = "";
                BufferedReader bufferedReader = new BufferedReader(new FileReader(old));
                for (int i = 0; i < 7; i++) {
                    info = bufferedReader.readLine();
                    geg[i] = info;
                }
                bufferedReader.close();
                geg[3] = newww0;
                return geg;

            } else System.out.println("Error...");
        } else System.out.println("Error....");

        return geg;
    }
    public static void wijzigdeel2(String[] gegevens, String pad, Integer nr) throws  IOException{
        String geb = list.get(nr).Gnaam;
        String naam = list.get(nr).name;
        File oud = new File(pad+"\\Account\\"+geb+"\\"+geb+".txt");
        File nieuw = new File(pad+"\\Account\\"+geb+"\\"+naam+".txt");
        FileWriter f = new FileWriter(nieuw);
        if (oud.delete()) {
            for (int i = 0; i < 7; i++) {
                f.write(gegevens[i]+"\n");
            }
            f.close();
            if (nieuw.renameTo(oud)) {
                System.out.println("Alles is netjes gewijzigd");

            } else System.out.println("Error.");
        }
        else
            System.out.println("Fout");
    }

最佳答案

java.io.File API 很旧,并且由于 API 设计及其各种实现而存在许多问题。在这种情况下,如果文件删除失败,File.delete 方法无法告诉您原因。

我建议您更改代码以使用 Java 1.7 中引入的较新的 java.nio.file.Pathjava.nio.file.Files API 。例如:

java.nio.file.Files

public static void delete(Path path) throws IOException

Deletes a file.

An implementation may require to examine the file to determine if the file is a directory. Consequently this method may not be atomic with respect to other file system operations. If the file is a symbolic link then the symbolic link itself, not the final target of the link, is deleted.

If the file is a directory then the directory must be empty. In some implementations a directory has entries for special files or links that are created when the directory is created. In such implementations a directory is considered empty when only the special entries exist. This method can be used with the walkFileTree method to delete a directory and all entries in the directory, or an entire file-tree where required.

On some operating systems it may not be possible to remove a file when it is open and in use by this Java virtual machine or other programs.

Parameters:

  • path - the path to the file to delete

Throws:

  • NoSuchFileException - if the file does not exist (optional specific exception)
  • DirectoryNotEmptyException - if the file is a directory and could not otherwise be deleted because the directory is not empty (optional specific exception)
  • IOException - if an I/O error occurs
  • SecurityException - In the case of the default provider, and a security manager is installed, the SecurityManager.checkDelete(String) method is invoked to check delete access to the file
<小时/>

我们无法确定为什么 File.delete 失败。但如果您使用 Files.delete 代替,您应该会得到一个异常,并且异常消息应该使问题的原因更清楚。

(FWIW,如果您使用的是 Windows,我的猜测是该文件被您自己的应用程序或其他应用程序锁定。这会导致删除失败。这是 Windows 特有的问题。)

关于java - File.delete() 方法不起作用..你能帮我吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60889618/

相关文章:

java - 文件名过滤器的使用

读取 .csv 文件时出现 java.io.FileNotFoundException

java - 带有 Java 外键的 MongoDb

Java 服务包装器和附加的应用程序命令行参数

java - 如何使用带有jsoup的类名从表标签中提取 "value"

java - 在 Jboss Wildfly 上部署 EAR 文件时失败

java - File.listFiles() 在 android 11 中返回 null

Java I/O 流;有什么区别?

java - 这是java教程中的错字吗?

java - 调用方法之前的 Java 泛型表示法是什么?