gpt4 book ai didi

java - File.delete() 不是实际的

转载 作者:行者123 更新时间:2023-12-01 19:50:00 26 4
gpt4 key购买 nike

我想删除文件夹及其内容。如果文件结构如下:

 TestFolder
-Folder1
--File1
-Folder2
--File2
--File3

数组应按以下顺序包含文件:{Folder1,Folder2,File1,Folder3,File2,File3},使用以下代码:

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.file.Files;

class Folder{
private int filesCount = 0;
private File[] files;
private int counter = 0;

private void getFilesArray() throws IOException{
paths = new String[countFiles(path)];
thisFolder(path);
}

private void countFiles(File folder) throws IOException{
File[] content = (new File(folder)).listFiles();
for (File file:content){
if (file.isDirectory()){
countFiles(file);
}
filesCount++;
}
}

private void thisFolder(File folder) throws IOException{
if (folder.exists()){
File[] content = file.listFiles();
for (File file:content){
files[counter] = file;
counter++;
if (file.isDirectory()){
thisFolder(file);
}
}
}else{
throw new IOException("Folder was deleted while it was still opened: "+file.getAbsolutePath());
}
}
//...
}

我想使用 File[]-Array 删除文件夹的内容:

public boolean delete(){
for (int count = filesCount-1; count >= 0;count--){
if(files[count].exists()){
if(files[count].delete()==false){
System.out.println("Failed to delete "+files[count]);
for (String temp:files[count].list())
System.out.println(" remaining content "+temp);
return false;
}else{
System.out.println("Properly deleted "+files[count].getAbsolutePathPath());
}
}
}
if (folder.delete()){
return true;
}else{
System.out.println(2);
return false;
}
}

我需要在多个进一步的方法中使用 File[]-Array,并且实际上希望我正在创建的文件夹对象包含我创建它时的(可能是旧的)内容。如果不工作,删除方法将返回 false。错误处理属于不同的类别。现在的问题是:

它会在第一次运行时删除所有文件属性以及在我运行程序之前为空的每个文件夹,但它无法删除在我运行程序之前包含内容的文件夹,即使它们现在是空的。

这里是调试输出(第一次运行):

//Properly deleted        Folder2\File3.txt
//Properly deleted Folder2\File2.txt
//Failed to delete Folder2
// remaining content File2.txt
// remaining content File3.txt
//program aborted

这里是调试输出(第二次运行):

//Properly deleted        Folder2
//Properly deleted Folder1\File1.txt
//Failed to delete Folder1
// remaining content File1.txt
//program aborted

这里是调试输出(第三次运行):

//Properly deleted        Folder1
//Properly deleted TestFolder
//program completed

该目录当然仅用于测试程序,因此我确信其中没有其他(隐藏)文件。对于测试来说,也没有使用文件夹内容的不同程序或方法,所以即使当我使用我编程的代码时可能会有所不同,对于这个测试,我绝对确定文件[ ]-数组是真实的!我什至从下面的评论测试了递归方法,但仍然遇到同样的问题,即使调试输出有点不同,因为我访问文件和文件夹的顺序不同。有什么技巧可以使文件再次变为实际吗?也许是像 File.update(); 这样的命令;

感谢您的帮助:)

最佳答案

你偏离了轨道。首先,放弃 sleep 。没必要。

接下来,您有多确定您正在按照正确的顺序进行删除?我个人不会按照你的方式删除它。我会创建一个递归方法来删除文件及其内容。

void deleteMe(File file) {
if (file.isDirectory()) {
for (File subfile: file.listFiles) {
deleteMe(subfile);
}
}
file.delete();
}

为了调试上面的代码,在调用删除之前,我可能会添加如下调试:

System.out.printf("File: %s. Number of subfiles: %d\n", file.getName(), file.listFiles().length).

(类似的东西。)

只是为了确保它正在做你认为它正在做的事情。

关于java - File.delete() 不是实际的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51736123/

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