首页 > 其他分享 >file文件分类工具,可将文件夹中的word和excel复制到指定文件夹

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

时间:2023-02-10 09:23:24浏览次数:44  
标签:复制到 word String failureTargetPath param fileName 文件夹 file

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

 

标签:复制到,word,String,failureTargetPath,param,fileName,文件夹,file
From: https://www.cnblogs.com/rdchen/p/17107783.html

相关文章

  • python3连接mysql报错RuntimeError: 'cryptography' package is required for sha256_
    使用pymysql报错RuntimeError‘cryptography‘packageisrequiredforsha256_passwordorcaching_sha2_passw如果报错:解决办法:安装cryptographypip3installcry......
  • CF319D Have You Ever Heard About the Word
    CF319DHaveYouEverHeardAbouttheWord好题,涉及复杂度分析,字符串结论,思维量较大。发现这种操作是不好同时进行的,我们只能较为“愚蠢”地模拟着操作,但是这种操作最多......
  • 群晖NAS如何装载远程文件夹
    通过远程映射文件夹的方式可以通过FileStation直接访问远程NAS内的资料,但不支持SMB映射。具体方法如下:​打开本地NAS——FileStation——工具——远程连接——连接设置。......
  • unity乃至的特殊文件夹
    Assets文件夹:游戏资源的顶层文件夹。AssetDatabase方法可以访问里面的任意资源。Editor文件夹:编辑模式下的代码需要放在这里。打包以后,会自动剥离它。EditorDefaultRes......
  • 利用宏感染 word 文档(基于MSF)
    1.生成我们的VBA代码msfvenom-ax86--platformwindows-pwindows/meterpreter/reverse_tcpLHOST=192.168.59.134LPORT=4444-ex86/shikata_ga_nai-i10-fvb......
  • 利用宏感染Word文档(基于MSF)
    1.生成我们的vba代码msfvenom-ax86--platformwindows-pwindows/meterpreter/reverse_tcpLHOST=IPLPORT=4444-ex86/shikata_ga_nai-i10-fvba-exe执行完成后......
  • 7、install_mysql_httpd_php_wordpress
    #!/bin/bash##********************************************************************#Author: zikang#QQ: [email protected]#Date: 2021-03-03......
  • 8、change_root_password
    #!/bin/bashrpm-qsshpass&>/dev/null||yum-yinstallsshpassexportSSHPASS=1NET=10.0.0foriin{1..7};do{ PASS=`opensslrand-b......
  • Bitwarden 正式收购开源创业公司 Passwordless.dev
    近些年黑客入侵企业系统、窃取企业关键数据、信息和源代码的事件频繁发生,很多时候黑客并没有使用什么高科技的手段,而是利用员工泄漏的账号登录了内部系统,从而获取了想要......
  • Python求取文件夹内的文件数量、子文件夹内的文件数量
      本文介绍基于Python语言,统计文件夹中文件数量;若其含有子文件夹,还将对各子文件夹中的文件数量一并进行统计的方法。  最近,需要统计多个文件夹内部的文件数量,包括其中......