1. 程式人生 > 實用技巧 >將某一資料夾下的檔案進行分類 並刪除一週之前的檔案

將某一資料夾下的檔案進行分類 並刪除一週之前的檔案

將某一資料夾下的檔案進行分類 新建目錄如下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 +"\t"+"複製檔案共計:"+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 +"\t"+"複製檔案共計:"+j+"\t"+"刪除檔案共計:"+ 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 +"\t"+"已分類共計:"+k +"\t"+"新複製檔案共計:"+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;
    }
}