gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:41:16 26 4
gpt4 key购买 nike

我想制作这个密码更改器,首先创建一个新文件,然后将原始文件中的数据放入 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com