package file;import java.io.File;/** * Output all files in a certain format in a folder* @author hasee * */public class Demo2 { public static void main(String[] args) { getTxtName("d:/a",".jpg"); } public static void getTxtName(String path,String suffix) { //Defend whether the file object is a file or a folder//Construct the file object File f = new File(path); //Defend if(f.isFile()) { if(f.getName().endsWith(suffix)) { System.out.println(f.getAbsolutePath()); } }else { //Transf the folder File[] files = f.listFiles(); if(files!=null && files.length>0) { //Continue to recursively obtain the file or folder for (File file : files) { getTxtName(file.getAbsolutePath(),suffix); } } } } } }2. Delete all files in a certain format under the folder
package file;import java.io.File;public class Demo3 { public static void main(String[] args) { // TODO Auto-generated method stub delete("d:/a",".jpg"); } public static void delete(String path,String suffix) { File f = new File(path); if(f.isFile()) { if(f.getName().endsWith(suffix)) { System.out.println(f.getAbsolutePath()+"Successfully delete"); f.delete(); } }else { File[] files = f.listFiles(); if(files!=null&&files.length>0) { for (File file : files) { delete(file.getAbsolutePath(),suffix); } } } } } }Summarize
The above is the example code of all file in a certain format under the Java implementation output folder introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!