Skip to content

Instantly share code, notes, and snippets.

@Viacheslav77
Created January 28, 2016 14:35
Show Gist options
  • Select an option

  • Save Viacheslav77/e504beeaf1d21e9ed02a to your computer and use it in GitHub Desktop.

Select an option

Save Viacheslav77/e504beeaf1d21e9ed02a to your computer and use it in GitHub Desktop.
Модифицироватьпроект FindFiles так, чтобы программа искала в каталоге все файлы с расширениями из списка
package FindFiles;
/* Модифицироватьпроект FindFiles так, чтобы программа искала в каталоге все файлы с расширениями из списка.*/
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
public class Main {
static class MyFileFilter implements FilenameFilter {
private String ext;
public MyFileFilter(String ext) {
this.ext = ext;
}
public boolean accept(File dir, String name) {
return name.endsWith(ext);
}
}
private static void findFiles(String srcPath, String [] listExp, ArrayList<String> list) throws IOException {
File dir = new File(srcPath);
for(int j = 0, i=0; j < listExp.length; j++,i++){
File[] files = dir.listFiles(new MyFileFilter(listExp[j]));
for( File f: dir.listFiles(new MyFileFilter(listExp[j]))){
list.add(f.getCanonicalPath());
}
}
}
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
String [] listExp = {"docx","java","txt"};
try {
findFiles("d:\\", listExp, list);
} catch (IOException e) {
e.printStackTrace();
}
for (String s : list)
System.out.println(s);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment