Delele 所有带有 Java 扩展名的文件



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

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")); 
        } 
}

获得文件的路径后,删除他:

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.
}

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

file.delete();

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

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

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

为每个要删除的文件调用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")); 
    } 
}
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();
        }
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));
       }
    }
}

最新更新