java常用类解析五:IO系统File类及文件搜索工具类

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

[java]   view plain copy
  1. <span style="font-size:16px;">package test;  
  2.   
  3. import java.io.File;  
  4. import java.io.FilenameFilter;  
  5. import java.util.Arrays;  
  6. import java.util.Scanner;  
  7. import java.util.regex.Pattern;  
  8.   
  9. /** 
  10.  * File代表文件和目录,静态域有:与系统有关的路径分隔符、与系统有关的默认名称分隔符。 
  11.  * 主要操作有:创建文件或目录、删除文件、给文件设定属性、返回指定目录下的文件列表、 
  12.  *          返回过滤后的文件列表、 检测文件是否存在、是否隐藏、是否是目录还是文件、 
  13.  *          返回文件名称和路径 
  14.  *  
  15.  * @author Touch 
  16.  * 
  17.  */  
  18. public class FileDemo {  
  19.     /* 
  20.      * 查找指定路径下的匹配regex的文件 
  21.      */  
  22.     public String[] find(String path, final String regex) {  
  23.         File file = new File(path);  
  24.         //匿名内部类  
  25.         return file.list(new FilenameFilter() {  
  26.             private Pattern pattern = Pattern.compile(regex);  
  27.             @Override  
  28.             public boolean accept(File dir, String name) {  
  29.                 // TODO Auto-generated method stub  
  30.                 return pattern.matcher(name).matches();  
  31.             }  
  32.         });  
  33.     }  
  34.   
  35.     public static void main(String[] args) {  
  36.         String path = null;  
  37.         String key = null;  
  38.         String regex = null;  
  39.         int choice = 1;  
  40.         Scanner scanner = new Scanner(System.in);  
  41.         System.out.println("please input the file path:");  
  42.         path = scanner.next();  
  43.         System.out.println("please input key:");  
  44.         key = scanner.next();  
  45.         System.out.println("choise:\n0:匹配以" + key + "为后缀的文件\n1:匹配包含" + key  
  46.                 + "的文件");  
  47.         if ((choice = scanner.nextInt()) == 0)  
  48.             regex = ".*\\." + key;  
  49.         else  
  50.             regex = ".*" + key + ".*";  
  51.         String[] list;  
  52.         list = new FileDemo().find(path, regex);  
  53.         System.out.println(Arrays.deepToString(list));  
  54.         //返回指定路径下的目录列表  
  55.         File[] fileList = new File(path).listFiles();  
  56.         for (File file : fileList) {  
  57.             if (file.isDirectory()) {  
  58.                 list = new FileDemo().find(file.getPath(), regex);  
  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 mine.util;  
  2.   
  3. import java.io.File;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6.   
  7. /** 
  8.  * FileDirectory类用于查找指定根目录下的所有文件和目录 可以通过正则表达式对要查找的 文件及目录进行筛选 
  9.  *  
  10.  * @author Touch 
  11.  */  
  12. public final class SearchFile {  
  13.     // 存放文件  
  14.     private List<File> fileList = new ArrayList<File>();  
  15.     // 存放目录  
  16.     private List<File> directoryList = new ArrayList<File>();  
  17.     // 存放文件和目录  
  18.     private List<File> list = new ArrayList<File>();  
  19.     private File file;// 目录  
  20.     private String regex;// 正则表达式  
  21.   
  22.     public SearchFile(String path) {  
  23.         file = new File(path);  
  24.         this.regex = ".*";  
  25.     }  
  26.   
  27.     public SearchFile(File file) {  
  28.         this.file = file;  
  29.         this.regex = ".*";  
  30.     }  
  31.   
  32.     public SearchFile(String path, String regex) {  
  33.         file = new File(path);  
  34.         this.regex = regex;  
  35.     }  
  36.   
  37.     public SearchFile(File file, String regex) {  
  38.         this.file = file;  
  39.         this.regex = regex;  
  40.     }  
  41.   
  42.     // 返回当前目录下的所有文件及子目录  
  43.     public List<File> files() {  
  44.         File[] files = file.listFiles();  
  45.         List<File> list = new ArrayList<File>();  
  46.         for (File f : files)  
  47.             if (f.getName().matches(regex))  
  48.                 list.add(f);  
  49.         return list;  
  50.     }  
  51.   
  52.     // 返回该根目录下的所有文件  
  53.     public List<File> allFiles() {  
  54.         if (list.isEmpty())  
  55.             search(file);  
  56.         return fileList;  
  57.     }  
  58.   
  59.     // 返回该根目录下的所有子目录  
  60.     public List<File> allDirectory() {  
  61.         if (list.isEmpty())  
  62.             search(file);  
  63.         return directoryList;  
  64.     }  
  65.   
  66.     // 返回该根目录下的所有文件及子目录  
  67.     public List<File> allFilesAndDirectory() {  
  68.         if (list.isEmpty())  
  69.             search(file);  
  70.         return list;  
  71.     }  
  72.   
  73.     // 递归搜索当前目录下的所有文件及目录  
  74.     private void search(File file) {  
  75.         File[] files = file.listFiles();  
  76.         if (files == null || files.length == 0)  
  77.             return;  
  78.         for (File f : files) {  
  79.             if (f.getName().matches(regex))  
  80.                 list.add(f);  
  81.             if (f.isFile() && f.getName().matches(regex))  
  82.                 fileList.add(f);  
  83.             else {  
  84.                 if (f.getName().matches(regex))  
  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 mine.util;  
  2.   
  3. import java.io.File;  
  4. import java.util.List;  
  5.   
  6. public class TestSearchFile {  
  7.     public static void main(String[] args) {  
  8.         System.out.println("-------- 指定目录中所有文件及子目录-------");  
  9.         List<File> list = (List<File>) new SearchFile(  
  10.                 "G:/java/workspace/test/file").files();  
  11.         for (File file : list)  
  12.             System.out.println(file.getName());  
  13.         System.out.println("--------指定目录中以txt为后缀的文件------");  
  14.         list = (List<File>) new SearchFile("G:/java/workspace/test/file",  
  15.                 ".*\\.txt").files();  
  16.         for (File file : list)  
  17.             System.out.println(file.getName());  
  18.         System.out.println("--------以该目录为根目录的所有文件及子目录--");  
  19.         list = (List<File>) new SearchFile("G:/java/workspace/test")  
  20.                 .allFilesAndDirectory();  
  21.         for (File file : list)  
  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

转载于:https://www.cnblogs.com/kinghitomi/archive/2012/01/19/2327392.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值