zoukankan      html  css  js  c++  java
  • 根据修改时间来获取文件

     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 }

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

  • 相关阅读:
    关于推荐的一个算法工程师访谈,有一些内容值得看看
    Element.Event
    复数输出
    Passenger/Nginx/Debian快速部署Rails
    POJ3678【错误总会让自己有收获的】
    android在其他线程中访问UI线程的方法
    C++运算符重载的方法
    Struts2图片文件上传,判断图片格式和图片大小
    list view Item 里面有ImageButton
    用python实现远程复制 (scp + expect )
  • 原文地址:https://www.cnblogs.com/ylink/p/5489605.html
Copyright © 2011-2022 走看看