How to find files with certain extension only
Written on January 20, 2010 at 7:11 am by
mkyong
Example
In this example, it will list out all the files end with “.jpg” extension in folder “c:\\folder”.
package com.mkyong.io; import java.io.*; public class FileChecker { private static final String FILE_DIR = "c:\\folder"; private static final String FILE_TEXT_EXT = ".jpg"; public static void main(String args[]) { new FileChecker().deleteFile(FILE_DIR,FILE_TEXT_EXT); } public void deleteFile(String folder, String ext){ GenericExtFilter filter = new GenericExtFilter(ext); File dir = new File(folder); //list out all the file name with .txt extension String[] list = dir.list(filter); if (list.length == 0) { System.out.println("no files end with : " + ext); return; } for (String file : list){ String temp = new StringBuffer(FILE_DIR) .append(File.separator) .append(file).toString(); System.out.println("file : " + temp); } } //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)); } } }
Oracle Magazine (Free)
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world\'s largest enterprise software company.
Publisher : Oracle Corporation



[...] Find files with certain extension only [...]