删除所有带有java扩展名的文件

时间:2012-06-28 17:08:10

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

7 个答案:

答案 0 :(得分: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.
}

答案 1 :(得分:1)

要删除文件,您可以调用delete功能

file.delete();

答案 2 :(得分:0)

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

答案 3 :(得分: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")); 
    } 
}

答案 4 :(得分:0)

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

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

答案 5 :(得分: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();
        }

答案 6 :(得分:-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));
       }
    }
}