1、先看一个File类的简单的例子

[java] view plain copy


1. <span style="font-size:16px;">package
2.   
3. import
4. import
5. import
6. import
7. import
8.   
9. /**
10.  * File代表文件和目录,静态域有:与系统有关的路径分隔符、与系统有关的默认名称分隔符。
11.  * 主要操作有:创建文件或目录、删除文件、给文件设定属性、返回指定目录下的文件列表、
12.  *          返回过滤后的文件列表、 检测文件是否存在、是否隐藏、是否是目录还是文件、
13.  *          返回文件名称和路径
14.  * 
15.  * @author Touch
16.  *
17.  */
18. public class
19. /*
20.      * 查找指定路径下的匹配regex的文件
21.      */
22. public String[] find(String path, final
23. new
24. //匿名内部类
25. return file.list(new
26. private
27. @Override
28. public boolean
29. // TODO Auto-generated method stub
30. return
31.             }  
32.         });  
33.     }  
34.   
35. public static void
36. null;  
37. null;  
38. null;  
39. int choice = 1;  
40. new
41. "please input the file path:");  
42.         path = scanner.next();  
43. "please input key:");  
44.         key = scanner.next();  
45. "choise:\n0:匹配以" + key + "为后缀的文件\n1:匹配包含"
46. "的文件");  
47. if ((choice = scanner.nextInt()) == 0)  
48. ".*\\."
49. else
50. ".*" + key + ".*";  
51.         String[] list;  
52. new
53.         System.out.println(Arrays.deepToString(list));  
54. //返回指定路径下的目录列表
55. new
56. for
57. if
58. new
59.                 System.out.println(Arrays.deepToString(list));  
60.             }  
61.         }  
62.     }  
63.   
64. }  
65. </span>



2、看完这个例子,是不是可以写个工具类呢,用于搜索指定路径下的所有文件或者目录,当然也可以输入正则表达式,这样就可以筛选出我们想要的文件(如有时候我们只需要.java文件或者.txt文件)




[java] view plain copy



    1. <span style="font-size:16px;">package
    2.   
    3. import
    4. import
    5. import
    6.   
    7. /**
    8.  * FileDirectory类用于查找指定根目录下的所有文件和目录 可以通过正则表达式对要查找的 文件及目录进行筛选
    9.  * 
    10.  * @author Touch
    11.  */
    12. public final class
    13. // 存放文件
    14. private List<File> fileList = new
    15. // 存放目录
    16. private List<File> directoryList = new
    17. // 存放文件和目录
    18. private List<File> list = new
    19. private File file;// 目录
    20. private String regex;// 正则表达式
    21.   
    22. public
    23. new
    24. this.regex = ".*";  
    25.     }  
    26.   
    27. public
    28. this.file = file;  
    29. this.regex = ".*";  
    30.     }  
    31.   
    32. public
    33. new
    34. this.regex = regex;  
    35.     }  
    36.   
    37. public
    38. this.file = file;  
    39. this.regex = regex;  
    40.     }  
    41.   
    42. // 返回当前目录下的所有文件及子目录
    43. public
    44.         File[] files = file.listFiles();  
    45. new
    46. for
    47. if
    48.                 list.add(f);  
    49. return
    50.     }  
    51.   
    52. // 返回该根目录下的所有文件
    53. public
    54. if
    55.             search(file);  
    56. return
    57.     }  
    58.   
    59. // 返回该根目录下的所有子目录
    60. public
    61. if
    62.             search(file);  
    63. return
    64.     }  
    65.   
    66. // 返回该根目录下的所有文件及子目录
    67. public
    68. if
    69.             search(file);  
    70. return
    71.     }  
    72.   
    73. // 递归搜索当前目录下的所有文件及目录
    74. private void
    75.         File[] files = file.listFiles();  
    76. if (files == null || files.length == 0)  
    77. return;  
    78. for
    79. if
    80.                 list.add(f);  
    81. if
    82.                 fileList.add(f);  
    83. else
    84. if
    85.                     directoryList.add(f);  
    86.                 search(f);  
    87.             }  
    88.         }  
    89.     }  
    90. }  
    91. </span>



    3、测试



    [java] view plain copy


    1. <span style="font-size:16px;">package
    2.   
    3. import
    4. import
    5.   
    6. public class
    7. public static void
    8. "-------- 指定目录中所有文件及子目录-------");  
    9. new
    10. "G:/java/workspace/test/file").files();  
    11. for
    12.             System.out.println(file.getName());  
    13. "--------指定目录中以txt为后缀的文件------");  
    14. new SearchFile("G:/java/workspace/test/file",  
    15. ".*\\.txt").files();  
    16. for
    17.             System.out.println(file.getName());  
    18. "--------以该目录为根目录的所有文件及子目录--");  
    19. new SearchFile("G:/java/workspace/test")  
    20.                 .allFilesAndDirectory();  
    21. for
    22.             System.out.println(file.getName());  
    23.     }  
    24. }  
    25. </span>




    4、结果:

    -------- 指定目录中所有文件及子目录-------
    aa.data
    bb.dat
    object
    test.txt
    test1.txt
    test2.txt
    test3.txt
    test4.txt
    test5
    --------指定目录中以txt为后缀的文件------
    test.txt
    test1.txt
    test2.txt
    test3.txt
    test4.txt
    --------以该目录为根目录的所有文件及子目录--
    .classpath
    .project
    .settings
    org.eclipse.jdt.core.prefs
    bin
    http
    PassWord.class
    Test.class
    mine
    util
    SearchFile.class
    TestSearchFile.class
    test
    A.class
    ArraysDemo.class
    B.class
    ByteArrayInputStreamDemo.class
    DataInputStreamAndByteArrayInputStreamDemo.class
    DataInputStreamDemo.class
    DeepCloneDemo.class
    FileDemo$1.class
    FileDemo.class
    FileInputStreamDemo.class

    1、先看一个File类的简单的例子


    [java] view plain copy


      1. <span style="font-size:16px;">package
      2.   
      3. import
      4. import
      5. import
      6. import
      7. import
      8.   
      9. /**
      10.  * File代表文件和目录,静态域有:与系统有关的路径分隔符、与系统有关的默认名称分隔符。
      11.  * 主要操作有:创建文件或目录、删除文件、给文件设定属性、返回指定目录下的文件列表、
      12.  *          返回过滤后的文件列表、 检测文件是否存在、是否隐藏、是否是目录还是文件、
      13.  *          返回文件名称和路径
      14.  * 
      15.  * @author Touch
      16.  *
      17.  */
      18. public class
      19. /*
      20.      * 查找指定路径下的匹配regex的文件
      21.      */
      22. public String[] find(String path, final
      23. new
      24. //匿名内部类
      25. return file.list(new
      26. private
      27. @Override
      28. public boolean
      29. // TODO Auto-generated method stub
      30. return
      31.             }  
      32.         });  
      33.     }  
      34.   
      35. public static void
      36. null;  
      37. null;  
      38. null;  
      39. int choice = 1;  
      40. new
      41. "please input the file path:");  
      42.         path = scanner.next();  
      43. "please input key:");  
      44.         key = scanner.next();  
      45. "choise:\n0:匹配以" + key + "为后缀的文件\n1:匹配包含"
      46. "的文件");  
      47. if ((choice = scanner.nextInt()) == 0)  
      48. ".*\\."
      49. else
      50. ".*" + key + ".*";  
      51.         String[] list;  
      52. new
      53.         System.out.println(Arrays.deepToString(list));  
      54. //返回指定路径下的目录列表
      55. new
      56. for
      57. if
      58. new
      59.                 System.out.println(Arrays.deepToString(list));  
      60.             }  
      61.         }  
      62.     }  
      63.   
      64. }  
      65. </span>




      2、看完这个例子,是不是可以写个工具类呢,用于搜索指定路径下的所有文件或者目录,当然也可以输入正则表达式,这样就可以筛选出我们想要的文件(如有时候我们只需要.java文件或者.txt文件)




      [java] view plain copy



      1. <span style="font-size:16px;">package
      2.   
      3. import
      4. import
      5. import
      6.   
      7. /**
      8.  * FileDirectory类用于查找指定根目录下的所有文件和目录 可以通过正则表达式对要查找的 文件及目录进行筛选
      9.  * 
      10.  * @author Touch
      11.  */
      12. public final class
      13. // 存放文件
      14. private List<File> fileList = new
      15. // 存放目录
      16. private List<File> directoryList = new
      17. // 存放文件和目录
      18. private List<File> list = new
      19. private File file;// 目录
      20. private String regex;// 正则表达式
      21.   
      22. public
      23. new
      24. this.regex = ".*";  
      25.     }  
      26.   
      27. public
      28. this.file = file;  
      29. this.regex = ".*";  
      30.     }  
      31.   
      32. public
      33. new
      34. this.regex = regex;  
      35.     }  
      36.   
      37. public
      38. this.file = file;  
      39. this.regex = regex;  
      40.     }  
      41.   
      42. // 返回当前目录下的所有文件及子目录
      43. public
      44.         File[] files = file.listFiles();  
      45. new
      46. for
      47. if
      48.                 list.add(f);  
      49. return
      50.     }  
      51.   
      52. // 返回该根目录下的所有文件
      53. public
      54. if
      55.             search(file);  
      56. return
      57.     }  
      58.   
      59. // 返回该根目录下的所有子目录
      60. public
      61. if
      62.             search(file);  
      63. return
      64.     }  
      65.   
      66. // 返回该根目录下的所有文件及子目录
      67. public
      68. if
      69.             search(file);  
      70. return
      71.     }  
      72.   
      73. // 递归搜索当前目录下的所有文件及目录
      74. private void
      75.         File[] files = file.listFiles();  
      76. if (files == null || files.length == 0)  
      77. return;  
      78. for
      79. if
      80.                 list.add(f);  
      81. if
      82.                 fileList.add(f);  
      83. else
      84. if
      85.                     directoryList.add(f);  
      86.                 search(f);  
      87.             }  
      88.         }  
      89.     }  
      90. }  
      91. </span>



      3、测试


      [java] view plain copy



        1. <span style="font-size:16px;">package
        2.   
        3. import
        4. import
        5.   
        6. public class
        7. public static void
        8. "-------- 指定目录中所有文件及子目录-------");  
        9. new
        10. "G:/java/workspace/test/file").files();  
        11. for
        12.             System.out.println(file.getName());  
        13. "--------指定目录中以txt为后缀的文件------");  
        14. new SearchFile("G:/java/workspace/test/file",  
        15. ".*\\.txt").files();  
        16. for
        17.             System.out.println(file.getName());  
        18. "--------以该目录为根目录的所有文件及子目录--");  
        19. new SearchFile("G:/java/workspace/test")  
        20.                 .allFilesAndDirectory();  
        21. for
        22.             System.out.println(file.getName());  
        23.     }  
        24. }  
        25. </span>



        4、结果:

        -------- 指定目录中所有文件及子目录-------
         aa.data
         bb.dat
         object
         test.txt
         test1.txt
         test2.txt
         test3.txt
         test4.txt
         test5
         --------指定目录中以txt为后缀的文件------
         test.txt
         test1.txt
         test2.txt
         test3.txt
         test4.txt
         --------以该目录为根目录的所有文件及子目录--
         .classpath
         .project
         .settings
         org.eclipse.jdt.core.prefs
         bin
         http
         PassWord.class
         Test.class
         mine
         util
         SearchFile.class
         TestSearchFile.class
         test
         A.class
         ArraysDemo.class
         B.class
         ByteArrayInputStreamDemo.class
         DataInputStreamAndByteArrayInputStreamDemo.class
         DataInputStreamDemo.class
         DeepCloneDemo.class
         FileDemo$1.class
         FileDemo.class
         FileInputStreamDemo.class