• 根据修改时间来获取文件


     1 public class Test {
     2     public static void main (String [] args) throws Exception{
     3 //        File file = new File("E:\tomcat\apache-tomcat-zhuyou\webapps\is");
     4         File newFile =new File("F:\comparetest");
     5 //        startdate = DateUtil.StringToDate("2016-05-11").getTime();
     6 //        enddate = DateUtil.StringToDate("2016-05-13").getTime();
     7 //        createFiles(file,newFile);
     8         delfiles(newFile);
     9         
    10     }
    11     /**
    12      * 复制文件
    13      * @throws Exception 
    14      */
    15     public static void copyfile(File file,File newFile) throws Exception{
    16         InputStream is = new FileInputStream(file);
    17         OutputStream os = new FileOutputStream(newFile);
    18         byte [] bytes = new byte[1024];
    19         int len;
    20         while((len = is.read(bytes))> 0){
    21             os.write(bytes, 0, len);
    22         }
    23         is.close();
    24         os.close();
    25     }
    26     public static Long startdate;
    27     public static Long enddate;
    28     /**
    29      * 比较时间
    30      */
    31     public static boolean comparedate(Long curdate){
    32         return curdate>=startdate && curdate<=enddate;
    33     }
    34     /**
    35      * 生成文件夹 符合条件的文件信息
    36      * @throws Exception 
    37      */
    38     public static void createFiles(File file,File newFile) throws Exception{
    39         File nfiles = new File(newFile,file.getName());
    40         if(!nfiles.exists()){
    41             nfiles.mkdir();
    42         }
    43         File [] files = file.listFiles();
    44         List<File> dfiles =new ArrayList<File>();
    45         for(File f: files){
    46             if(f.isDirectory()){
    47                 dfiles.add(f);
    48             }else{
    49                 if(comparedate(f.lastModified())){
    50                     copyfile(f,new File(nfiles,f.getName()));
    51                 }
    52             }
    53         }
    54         for(File fs : dfiles){
    55             createFiles(fs,nfiles);
    56         }
    57     }
    58     /**
    59      * 删除没有内容的文件夹
    60      */
    61     public static void delfiles(File file)throws Exception{
    62         File[] files = file.listFiles();
    63         if(files.length == 0){
    64             file.deleteOnExit();
    65         }else{
    66             List<File> lists = new ArrayList<File>();
    67             for(File f : files){
    68                 if(f.isDirectory()){
    69                     delfiles(f);
    70                 }
    71             }
    72         }
    73         
    74     }
    75 }

    根据修改时间来获取在某个时间段的文件 

  • 相关阅读:
    CSS浏览器兼容解决总结
    Access的数据操作类(带异常处理)
    网页消息类
    生成验证码(纯中文)
    C#连接各种数据库(持续更新中)
    asp.net中实现文件批量上传!你会了吗?
    http 错误编号大全(转)
    SQL SERVER数据操作类
    web.config加密和解密
    C#中操作CMD命令行窗口
  • 原文地址:https://www.cnblogs.com/ylink/p/5489605.html
Copyright © 2020-2023  润新知