Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. /*
  2. Модифицировать проект FindFiles так, чтобы программа искала в каталоге
  3. */
  4. package ua.mralex;
  5.  
  6. import java.io.File;
  7. import java.io.FilenameFilter;
  8. import java.io.IOException;
  9. import java.util.ArrayList;
  10.  
  11. public class Main {
  12.  
  13. static class MyFileFilter implements FilenameFilter {
  14.  
  15. private String[] ext;
  16.  
  17. public MyFileFilter(String[] ext) {
  18. this.ext = ext;
  19. }
  20.  
  21. @Override
  22. public boolean accept(File dir, String name) {
  23. for (String ex : ext) {
  24. if (name.endsWith(ex)) {
  25. return true;
  26. }
  27. }
  28. return false;
  29. }
  30. }
  31.  
  32. private static void findFiles(String srcPath, String[] ext, ArrayList<String> list) throws IOException {
  33. File dir = new File(srcPath);
  34. File[] files = dir.listFiles(new MyFileFilter(ext));
  35.  
  36. for (File f : files) {
  37. list.add(srcPath + f.getName());
  38. }
  39. }
  40.  
  41. public static void main(String[] args) {
  42. ArrayList<String> list = new ArrayList<>();
  43. String[] ext = {"txt", "docx", "rar", "xlsx"};
  44.  
  45. try {
  46. findFiles("c:\\", ext, list);
  47. } catch (IOException e) {
  48. }
  49.  
  50. for (String s : list) {
  51. System.out.println(s);
  52. }
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement