2012-06-28 180 views
-2

因此,我发现前面的一些代码看起来像它会工作,但它不会调用删除文件来列出它们。我需要添加什么才能删除文件?删除在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")); 
     } 
} 
+5

您需要添加'file.delete();' –

回答

-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)); 
     } 
    } 
} 
+1

为什么复杂,没有解释,你已经给了简单的答案?然后接受你自己的答案? – Dave

+0

非其他人为我工作,简单与否。 – jerhynsoen

+0

从上面的答案引用:'啊,这就是我一直在寻找的。谢谢! - jerhynsoen'听起来像是对我的“答案” – Dave

1

要删除一个文件,你可以调用delete功能

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

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

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

啊,这就是我一直在寻找。谢谢! – jerhynsoen

+0

你可以做的另一件事是使用String.endsWith(字符串),而不是该正则表达式... – DankMemes

0

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

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

+0

诱惑下来投票。你通过编写代码来学习,然后弄清楚你做错了什么。 – Dave

+0

我同意。阅读文档有帮助,但您可以通过实际编码和提问问题来了解更多信息。 – DankMemes

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(); 
     } 
+0

您应该为FilenameUtils-Class导入commons-io – Mirko

相关问题