-
-
Save Viacheslav77/e504beeaf1d21e9ed02a to your computer and use it in GitHub Desktop.
Модифицироватьпроект FindFiles так, чтобы программа искала в каталоге все файлы с расширениями из списка
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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