-2

所以我之前发现了一些看起来可以工作的代码,但它不会调用删除文件只是为了列出它们。我需要添加什么才能删除文件?

import java.io.File;
import java.util.regex.Pattern;

public class cleardir {
    static String userprofile = System.getenv("USERPROFILE"); 

    private static void walkDir(final File dir, final Pattern pattern) {   
        final File[] files = dir.listFiles();   
        if (files != null) {     
            for (final File file : files) {       
                if (file.isDirectory()) {         
                    walkDir(file, pattern);       
                    } else if (pattern.matcher(file.getName()).matches()) { 
                        System.out.println("file to delete: " + file.getAbsolutePath());  
                        }     }   } }  
    public static void main(String[] args) {   
        walkDir(new File(userprofile+"/Downloads/Software_Tokens"), 
                Pattern.compile(".*\\.sdtid")); 
        } 
}
4

7 回答 7

2

一旦你有了文件的路径,删除他:

File physicalFile = new File(path); // This is one of your file objects inside your for loop, since you already have them just delete them.
try {
    physicalFile.delete(); //Returns true if the file was deleted or false otherwise. 
                           //You might want to know this just in case you need to do some additional operations based on the outcome of the deletion.
} catch(SecurityException securityException) {
    //TODO Handle. 
    //If you haven't got enough rights to access the file, this exception is thrown.
}
于 2012-06-28T17:11:34.387 回答
1

要删除文件,您可以调用该delete函数

file.delete();
于 2012-06-28T17:11:32.613 回答
0

您可以在 File 的实例上调用 delete() 方法。请务必检查返回码以确保您的文件已被实际删除。

于 2012-06-28T17:12:09.703 回答
0

调用File.delete()您要删除的每个文件。所以你的代码是:

import java.io.File;
import java.util.regex.Pattern;

public class cleardir {
    static String userprofile = System.getenv("USERPROFILE"); 

    private static void walkDir(final File dir, final Pattern pattern) {   
        final File[] files = dir.listFiles();   
        if (files != null) {     
            for (final File file : files) {       
                if (file.isDirectory()) {         
                    walkDir(file, pattern);       
                    } else if (pattern.matcher(file.getName()).matches()) { 
                        System.out.println("file to delete: " + file.getAbsolutePath()); 
                        boolean deleteSuccess=file.delete();
                        if(!deleteSuccess)System.err.println("[warning]: "+file.getAbsolutePath()+" was not deleted...");
                    }
                }
             }
         }

public static void main(String[] args) {   
    walkDir(new File(userprofile+"/Downloads/Software_Tokens"), 
            Pattern.compile(".*\\.sdtid")); 
    } 
}
于 2012-06-28T17:12:34.967 回答
0

用于file.delete();删除文件。

在尝试编写程序之前,您需要正确学习 Java 基础知识。好资源:http ://docs.oracle.com/javase/tutorial/index.html

于 2012-06-28T17:13:26.010 回答
0
final File folder = new File("C:/Temp");
        FileFilter ff = new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                String ext = FilenameUtils.getExtension(pathname.getName());
                return ext.equalsIgnoreCase("EXT"); //Your extension
            }
        };
        final File[] files = folder.listFiles(ff);
        for (final File file : files) {
            file.delete();
        }
于 2012-07-23T06:50:55.820 回答
-4
public class cleardir {
static String userprofile = System.getenv("USERPROFILE");
   private static final String FILE_DIR = userprofile+"\\Downloads\\Software_Tokens";
   private static final String FILE_TEXT_EXT = ".sdtid";

   public static void run(String args[]) {
    new cleardir().deleteFile(FILE_DIR,FILE_TEXT_EXT);
   }

   public void deleteFile(String folder, String ext){

     GenericExtFilter filter = new GenericExtFilter(ext);
     File dir = new File(folder);
     if (dir.exists()) { 
     //list out all the file name with .txt extension
     String[] list = dir.list(filter);

     if (list.length == 0) return;

     File fileDelete;

     for (String file : list){
    String temp = new StringBuffer(FILE_DIR)
                      .append(File.separator)
                      .append(file).toString();
        fileDelete = new File(temp);
        boolean isdeleted = fileDelete.delete();
        System.out.println("file : " + temp + " is deleted : " + isdeleted);
     }
   }
   }
   //inner class, generic extension filter 
   public class GenericExtFilter implements FilenameFilter {

       private String ext;

       public GenericExtFilter(String ext) {
         this.ext = ext;             
       }

       public boolean accept(File dir, String name) {
         return (name.endsWith(ext));
       }
    }
}
于 2012-07-06T22:14:41.550 回答