file文件分类工具,可将文件夹中的word和excel复制到指定文件夹

package com.xxx.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author 38962 文件分类工具,可将文件夹中的Word和excel复制到目标文件夹
 */
public class CheckoutExcelOrWord {
    // private static Map<String,Integer> map = new HashMap<>();

    /**
     * @param sourceFile
     *            资源路径
     * @param excelTargetPath
     *            excel文件夹路径
     * @param wordTargetPath
     *            word文件夹路径
     * @param failureTargetPath
     *            作废文件 文件夹路径
     * @throws Exception
     */
    public static void checkFileType(File sourceFile, String excelTargetPath, String wordTargetPath, String failureTargetPath) throws Exception {
        // 若源文件是一个文件夹,那么遍历该文件夹下所有的子文件
        if (sourceFile.isDirectory() && !sourceFile.getName().contains("废")) {
            File[] files = sourceFile.listFiles();
            for (File file : files) {
                // 若该文件为文件,并且是word文件,那么保存到目标文件中
                if (file.isFile()) {
                    isExcel(file, excelTargetPath, failureTargetPath);
                    isWord(file, wordTargetPath, failureTargetPath);
                } else {
                    // 若该文件为文件夹,那么进行递归
                    checkFileType(file, excelTargetPath, wordTargetPath, failureTargetPath);
                }
            }
        }
    }

    /**
     * @param file
     * @param targetPath
     * @param failureTargetPath
     * @throws Exception
     */
    public static void isExcel(File file, String targetPath, String failureTargetPath) throws Exception {
        String fileName = file.getName();
        if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) {
            if (!fileName.contains("废")) {
                copyFile(file, targetPath);
            } else {
                copyFile(file, failureTargetPath);
            }
        }
    }

    /**
     * @param file
     * @param targetPath
     * @param failureTargetPath
     * @throws Exception
     */
    public static void isWord(File file, String targetPath, String failureTargetPath) throws Exception {
        String fileName = file.getName();
        if (fileName.endsWith(".doc") || fileName.endsWith(".docx")) {
            if (!fileName.contains("废")) {
                copyFile(file, targetPath);
            } else {
                copyFile(file, failureTargetPath);
            }
        }
    }

    /**
     * @param file
     * @param targetPath
     * @throws IOException
     */
    public static void copyFile(File file, String targetPath) throws IOException {
        String fileName = file.getName();
        FileInputStream inputStream = new FileInputStream(file);
        // 目标文件对象
        FileOutputStream outputStream = new FileOutputStream(targetPath + "\\" + fileName);
        int len = -1;
        byte[] buff = new byte[1024];
        while ((len = inputStream.read(buff)) != -1) {
            outputStream.write(buff, 0, len);
        }
        inputStream.close();
        outputStream.close();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {

        
        String sourcePath = "D:\\ytd\\dzd\\所有定值单";
        String excelTargetPath = "D:\\ytd\\dzd\\excel定值单";
        String wordTargetPath = "D:\\ytd\\dzd\\word定值单";
        String failureTargetPath = "D:\\ytd\\dzd\\作废定值单";

        File sourceFile = new File(sourcePath);
        try {
            System.out.println("拷贝中...");
            checkFileType(sourceFile, excelTargetPath, wordTargetPath, failureTargetPath);
            System.out.println("拷贝结束");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
View Code

 

posted @ 2023-02-10 09:13  爱跳舞的程序员  阅读(103)  评论(0编辑  收藏  举报