用字符流实现每个文件夹中创建包含所有文件信息的readme.txt

  1 package com.readme;
  2 
  3 import java.io.BufferedWriter;
  4 import java.io.File;
  5 import java.io.FileWriter;
  6 import java.io.IOException;
  7 import java.text.SimpleDateFormat;
  8 import java.util.Date;
  9 import java.util.Scanner;
 10 
 11 /**
 12  * @author lisj
 13  * 用字符流实现每个文件夹中创建包含所有文件信息的readme.txt
 14  *
 15  */
 16 public class FileNews {
 17 
 18     static int i=0;    //定义文件个数全局变量
 19     public static void main(String args[]) throws IOException{
 20         
 21         find();  //调用查找文件函数        
 22         System.out.println("共有"+i+"个文件! ");
 23         
 24     }
 25 
 26     
 27     /**
 28      * find()函数实现目录参数的传递
 29      * 在此方法中调用写文件信息函数
 30      */
 31     private static void find() throws IOException {
 32         
 33         Scanner scanner=new Scanner(System.in);
 34         System.out.println("请输入目录(输入end退出程序):");        
 35         String findpath=scanner.next();        //输入路径
 36         
 37         
 38         File file=new File(findpath);
 39         
 40         if(!file.exists()||!file.isDirectory()){    //判断该路径是否存在,是否是目录
 41             
 42             if(findpath.equals("end"))            //结束判断条件
 43             {
 44                 System.out.println("程序结束,感谢使用!");
 45                 System.exit(-1);
 46             }
 47             else        //输入错误,重新输入
 48             {
 49                 System.out.println("输入的路径不存在,请重新输入!(输入end退出程序)");
 50                 find();        //递归调用
 51             }
 52                         
 53         }
 54         
 55         lookup(file);    //调用判断文件,写文件信息函数
 56         
 57     }
 58     
 59     
 60     
 61     /**
 62      * lookup()函数实现目标目录下文件和文件夹的判别
 63      * 在此方法中实现写入文件信息到readme.txt
 64      */
 65     public static void lookup(File file) throws IOException{
 66         
 67         String read=file.getAbsolutePath();
 68         File readfileexist=new File(read+"/"+"readme.txt"); 
 69         if(readfileexist.exists()){    //如果已经存在readme.txt,则删除
 70             readfileexist.delete();
 71         }
 72         
 73         File[] names=file.listFiles();    //输入的路径下的文件的目录
 74             
 75         BufferedWriter out=null;    //定义字符输出流
 76         
 77         //查找到的文件属性数组的初始化
 78         if(names!=null){
 79         
 80         for(File name:names){        //遍历输入的路径下的文件和文件夹
 81             
 82             if(name.isFile())        //判断该路径下是文件
 83             {        
 84                 
 85                     //定义文件时间属性                    
 86                     Date date=new Date(name.lastModified());
 87                     SimpleDateFormat simpledate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 88                     
 89                     String laststr  = name.getParent();
 90                     File readfile=new File(laststr+"/"+"readme.txt"); //定义字符流写入的路径和文件名
 91                 
 92                     out = new BufferedWriter(new FileWriter(readfile,true));  //定义字符流可追加写入信息                
 93                                 
 94                                 out.write("名字:"+name.getName()+"   ");         //写入文件信息
 95                                 out.newLine();
 96                                 out.write("大小:"+name.length()+"  字节");
 97                                 out.newLine();
 98                                 out.write("时间:"+simpledate.format(date)+"  ");    
 99                                 out.newLine();
100                                 out.newLine();
101                                 out.flush();        //刷新缓冲以及关闭流
102                                 out.close();
103                                 i++;            //统计文件个数
104                         
105                 }
106                 else
107                 {
108                     lookup(name);//递归调用函数
109                 }
110             }
111         
112         }
113 
114     }
115         
116 }
原文地址:https://www.cnblogs.com/lifescolor/p/3874080.html