java - deleting the specific file in a directory and all sub directories -


this question has answer here:

am using code deleting specific file in directories , sub directorates in drive not working. please me in regard. if want delete specific file in drives how do.

   static string refile= "input.txt";       public static void deletemyfile(file directory) {     if (directory.exists()) {            file[] files = directory.listfiles();            if (null != files) {                  (int = 0; < files.length; i++) {                         system.out.println(files[i].getname());                         if (files[i].isdirectory()) {                                deletemyfile(files[i]);                          } else  {                               string temp ;                             temp = files[i].getname();                                                           if (temp==refile){                              system.out.println("name matched , delete");                              (files[i]).delete();                           } else{                              system.out.println("name not matched");                         }                            }                  }            }     } else {system.out.println("wrong path");     }   } 

try using java 8 solution. following walk through sub-directories given directory , delete files match given filename.

public static void deletemyfile(file directory, string filename) {     if(directory.isdirectory()) {         try {             files.walk(directory.topath())                 .filter(path -> path.getfilename().tostring().equals(filename))                 .foreach(path -> path.tofile().delete());         }         catch(exception e) {             e.printstacktrace();         }     } } 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -