zoukankan      html  css  js  c++  java
  • util_文件分类工具(可将文件夹中的Word和excel复制到指定文件夹)

    package com.ytd.ebos.platform.dzdglyqh.controller;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    /**
     * @author 38962 文件分类工具,可将文件夹中的Word和excel复制到目标文件夹
     */
    public class CheckoutExcel {
        // 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 = "F:\\gdytd\\mzzdjs_source\\初始化定值单\\dzd";
            String excelTargetPath = "F:\\gdytd\\mzzdjs_source\\初始化定值单\\excel定值单";
            String wordTargetPath = "F:\\gdytd\\mzzdjs_source\\初始化定值单\\word定值单";
    
            String failureTargetPath = "F:\\gdytd\\mzzdjs_source\\初始化定值单\\拷贝出来的---作废定值单";
    
            File sourceFile = new File(sourcePath);
            try {
                System.out.println("拷贝中...");
                checkFileType(sourceFile, excelTargetPath, wordTargetPath, failureTargetPath);
                System.out.println("拷贝结束");
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    View Code
  • 相关阅读:
    Go语言 函数return值的几种情况
    VSCode 常用快捷键 Mac
    Java异常处理
    Java面向对象《三》
    C++类模板
    C++函数模板
    盛最多水的容器
    实现 Trie (前缀树)
    排序链表
    类似某团app搜索城市界面中 点击右侧城市首字母,对应城市区域置顶的功能(uniapp)
  • 原文地址:https://www.cnblogs.com/rdchen/p/15792869.html
Copyright © 2011-2022 走看看