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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱跳舞的程序员.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值