Java I/O---获取文件目录并写入到文本

        首先获取指定目录下的所有文件目录,存入List集合中,然后创建文本文件将List遍历写入文本中保存。

1.主程序类

  1 public class Test {
  2 
  3 	/**
  4 	 * @param args
  5 	 */
  6 	public static void main(String[] args) {
  7 		// TODO Auto-generated method stub
  8 		//获取IO目录下的所有java文件
  9 		File dir = new File("H:\\workspace\\IO");
 10 
 11 		List<File> list = fileList(dir,".java");//路径列表,传入过滤器
 12 
 13 		//获取路径下的符合条件的文件后,写入一个txt中
 14 		File destFile = new File("H:\\workspace\\Testfile\\javaList.txt");
 15 		
 16             write2File(list,destFile);
 17 	}

 

2.过滤文件的方法,传入指定 路径参数 和文件 后缀参数, 返回文件List集合

  1 
  2     /**
  3 	 * 定义获取指定过滤器条件的集合
  4 	 * @param dir 路径
  5 	 * @param string 后缀.java
  6 	 * @return
  7 	 */
  8 	public static List<File> fileList(File dir, String suffix){
  9 		//1.定义集合
 10 		List<File> list = new ArrayList<File>();
 11 
 12 		//2.过滤器
 13 		FileFilter filter = new  FileFilterBySuffix(suffix);
 14 		getFileList(dir, list, filter);
 15 
 16 		return list;
 17 
 18 	}

3.过滤器方法和过滤器类

  1 	/**
  2 	 * 定义一个获取指定过滤器条件的集合
  3 	 * 多级目录下,传递list
  4 	 * @param dir 路径
  5 	 * @param list 文件集合
  6 	 * @param filter 过滤器
  7 	 */
  8 	public static void getFileList(File dir, List<File> list, FileFilter filter){
  9 		File[] files = dir.listFiles();
 10 
 11 		for(File file : files){
 12 			if(file.isDirectory()){
 13 				getFileList(file, list, filter);//递归
 14 			}else{
 15 				//过滤文件
 16 				if(filter.accept(file)){
 17 					list.add(file);//添加
 18 				}
 19 			}
 20 		}
 21 	}
 22 }
 23 
  1 public class FileFilterBySuffix implements FileFilter {
  2 
  3 	private String suffix;
  4 
  5 	public FileFilterBySuffix(String suffix) {
  6 		super();
  7 		this.suffix = suffix;
  8 	}
  9 
 10 	@Override
 11 	public boolean accept(File pathname) {
 12 		// TODO Auto-generated method stub
 13 		return pathname.getName().endsWith(suffix);
 14 	}
 15 
 16 }

4.写入list到文件方法

  1 	/**
  2 	 * 将list写入txt中
  3 	 * @param list 文件列表
  4 	 * @param destFile 存储对象java文件列表
  5 	 */
  6 	private static void write2File(List<File> list, File destFile) {
  7 		// TODO Auto-generated method stub
  8 		BufferedWriter bufw = null;
  9 
 10 		try {
 11 			//使用字符缓冲区对象BufferedWriter
 12 			bufw = new BufferedWriter(new FileWriter(destFile));
 13 
 14 			//遍历list,写入绝对路径
 15 			for(File file : list){
 16 				bufw.write(file.getAbsolutePath());//写入绝对路径
 17 				bufw.newLine();//换行
 18 				bufw.flush();  //刷新纪录
 19 			}
 20 
 21 		} catch (Exception e) {
 22 			// TODO: handle exception
 23 		} finally{
 24 			if(bufw != null){
 25 				try {
 26 					bufw.close();
 27 				} catch (IOException e) {
 28 					// TODO Auto-generated catch block
 29 					throw new RuntimeException();
 30 				}
 31 			}
 32 		}
 33 	}

转载于:https://www.cnblogs.com/zhuweiheng/p/8149033.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值