java - deleting the specific file in a directory and all sub directories -
this question has answer here:
- how compare strings in java? 23 answers
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
Post a Comment