将某一文件夹下的文件进行分类 并删除一周之前的文件

将某一文件夹下的文件进行分类   新建目录如下yyyy/MM/dd/xx.jpg 

写法一:   (带选定指定含有某些字符的文件)

import java.io.*;

import static java.lang.System.out;

/**
 * @description
 * @date 2020/9/8
 */
public class FileCopy2 {

    //目标源文件
    public static File dirFrom;
    //目标新文件
    public static File dirTo;

    /**
     *
     * @param file 目标文件夹
     * @param filter  过滤字符  yyyyMMdd
     */
    public void listFileInDir(File file,String filter){
        File dirTos = null;
        File[] files = file.listFiles((dir, name) -> name.contains(filter));
        //设备编号
        String equipment = null;
        //当前年 月 日
        String year = filter.substring(0,4);
        String mouth = filter.substring(4,6);
        String day = filter.substring(6,8);

        int i = 0;
        for (File f : files) {
            //文件名称
            String fileName = f.getName();
            equipment = fileName.substring(0,3);
            dirTos = new File(dirTo + "\" + year + "\" + mouth + "\" + day + "\" + equipment);

            String tempform = f.getAbsolutePath();
            //后面的路径  替换前面的路径
            String tempto = tempform.replace(dirFrom.getAbsolutePath(),dirTos.getAbsolutePath());
            if(f.isDirectory()){
                File tempFile = new File(tempto);
                tempFile.mkdirs();
                listFileInDir(f,filter);
            }else{
                out.println("源文件"+  f.getAbsolutePath());
                int endindex = tempto.lastIndexOf("\");//找到"/"所在的位置
                String mkdirPath = tempto.substring(0, endindex);
                File tempFile = new File(mkdirPath);
                tempFile.mkdirs();//创建文件夹
                out.println("目标点:"+tempto);
                copy(tempform,tempto);
                i++;
            }
        }
        System.out.println("目标新文件共计:"+files.length +"	"+"复制文件共计:"+i);
    }


    /**
     * 文件拷贝
     */
    public void copy(String from, String to){

        try {
            FileInputStream in = new FileInputStream(from);
            FileOutputStream out = new FileOutputStream(to);

            byte[] buff = new byte[1024];
            int len = 0;
            while ((len = in.read(buff)) != -1){
                out.write(buff,0,len);
            }
            in.close();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

写法二:复制文件在新的目录下 ,并删除目标文件夹中一周之前的文件

import java.io.*;
import java.util.Calendar;
import java.util.Date;

import static java.lang.System.out;

/**
 * @description
 * @date 2020/9/8
 */
public class FileCopy {

    //目标源文件
    public static File dirFrom;
    //目标新文件
    public static File dirTo;

    /**
     *  针对过滤字符
     * @param file 目标文件夹
     * @param filter  过滤字符
     */
    public void listFileInDir(File file,String filter){
        File dirTos = null;
        File[] files = file.listFiles();
        //设备编号
        String equipment = null;
        //当前年 月 日
        String year = filter.substring(0,4);
        String mouth = filter.substring(4,6);
        String day = filter.substring(6,8);

        int j = 0;//复制文件数
        int l = 0;//删除文件数
        for (File f : files) {
            //文件名称
            String fileName = f.getName();
            if(!fileName.contains(filter)){//不符合条件的进行判断是否是一周之前的是的话进行删除
                Date today = new Date();
                long time=f.lastModified();
                Calendar cal=Calendar.getInstance();
                cal.setTimeInMillis(time);
                Date lastModified = cal.getTime();
                long days = getDistDates(today, lastModified);
                if(days>7){
                    f.delete();
                    l++;
                }
            }else{//符合条件的进行复制分类
                equipment = fileName.substring(0,3);
                dirTos = new File(dirTo + "\" + year + "\" + mouth + "\" + day + "\" + equipment);

                String tempform = f.getAbsolutePath();
                //后面的路径  替换前面的路径
                String tempto = tempform.replace(dirFrom.getAbsolutePath(),dirTos.getAbsolutePath());
                if(f.isDirectory()){
                    File tempFile = new File(tempto);
                    tempFile.mkdirs();
                    listFileInDir(f,filter);
                }else{
                    out.println("源文件"+  f.getAbsolutePath());
                    int endindex = tempto.lastIndexOf("\");//找到"/"所在的位置
                    String mkdirPath = tempto.substring(0, endindex);
                    File tempFile = new File(mkdirPath);
                    tempFile.mkdirs();//创建文件夹
                    out.println("目标点:"+tempto);
                    copy(tempform,tempto);
                    j++;
                }
            }
        }
        System.out.println("目标文件共计:"+files.length +"	"+"复制文件共计:"+j+"	"+"删除文件共计:"+ l);
    }


    /**
     *  将所有文件进行分类
     * @param file 目标文件夹
     */
    public void listFileInDir(File file){
        File dirTos = null;
        File[] files = file.listFiles();
        //设备编号
        String equipment = null;
        //当前年 月 日
        String year = null;
        String mouth = null;
        String day = null;

        int n = files.length;//总数
        int j = 0;//复制文件数
        int k = 0;//被复制文件存在个数
        for (File f : files) {
            //文件名称
            String fileName = f.getName();
            equipment = fileName.substring(0,3);
            year = fileName.substring(4,8);
            mouth = fileName.substring(8,10);
            day = fileName.substring(10,12);
            dirTos = new File(dirTo + "\" + year + "\" + mouth + "\" + day + "\" + equipment);
            String tempform = f.getAbsolutePath();
            //后面的路径  替换前面的路径
            String tempto = tempform.replace(dirFrom.getAbsolutePath(),dirTos.getAbsolutePath());
            if(f.isDirectory()){
                File tempFile = new File(tempto);
                tempFile.mkdirs();
                listFileInDir(f);
            }else{
                File efile = new File(tempto);
                if(efile.exists()){
                    k++;
                    continue;
                }
                int endindex = tempto.lastIndexOf("\");//找到"/"所在的位置
                String mkdirPath = tempto.substring(0, endindex);
                File tempFile = new File(mkdirPath);
                tempFile.mkdirs();//创建文件夹
                copy(tempform,tempto);
                j++;
            }
            out.println(j);
        }
        System.out.println("目标文件共计:"+ n +"	"+"已分类共计:"+k +"	"+"新复制文件共计:"+j);
    }

    /**
     * 文件拷贝
     */
    public void copy(String from, String to){

        try {
            FileInputStream in = new FileInputStream(from);
            FileOutputStream out = new FileOutputStream(to);

            byte[] buff = new byte[1024];
            int len = 0;
            while ((len = in.read(buff)) != -1){
                out.write(buff,0,len);
            }
            in.close();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * @param startDate
     * @param endDate
     * @return
     */
    public static long getDistDates(Date startDate, Date endDate)
    {
        long totalDate = 0;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        long timestart = calendar.getTimeInMillis();
        calendar.setTime(endDate);
        long timeend = calendar.getTimeInMillis();
        totalDate = Math.abs((timeend - timestart))/(1000*60*60*24);
        return totalDate;
    }

  
  public static void main(String[] args) {
  File formFile = new File("d:\test\");
  File toFile = new File("d:\testF\");
  String filter = "20200909";

   FileCopy fileCopy = new FileCopy();
  fileCopy.dirFrom = formFile;
  fileCopy.dirTo = toFile;
  fileCopy.listFileInDir(formFile,filter);
  }
}
原文地址:https://www.cnblogs.com/HHbJ/p/13645614.html