复制文件夹和文件取敏感词

package com.wxy.files;

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

public class Files {

    public static void main(String[] args) throws Exception {
        Files fs = new Files();
        fs.traverseFolder2("F:/wxy/");
    }
    
    public void traverseFolder2(String path) throws Exception {
        
        File source;
        File dest;

        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (files.length == 0) {
                System.out.println("文件夹是空的!");
                return;
            } else {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
                        System.out.println("文件夹:" + file2.getAbsolutePath());
                        //获得文件夹名
                        File filejia =new File(file2.getAbsolutePath().trim());  
                        String name = filejia.getName();
                        System.out.println("feilejia:"+name);
                        //判断文件夹名包含敏感词汇就把这个文件夹复制出来
                        if(name.contains("毛泽东")){
                            //需要复制的目标文件或目标文件夹
                            String pathname = file2.getAbsolutePath();
                            File filejia1 = new File(pathname);
                            //复制到的位置
                            String topathname = "F:/文件存放";
                            File toFile = new File(topathname);
                            try {
                                copyfilejia(filejia1, toFile);
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            continue;
                        }
                        
                        traverseFolder2(file2.getAbsolutePath());
                    } else {
                        System.out.println("文件:" + file2.getAbsolutePath());
                        //获取文件名
                        File tempFile =new File(file2.getAbsolutePath().trim());  
                        String fileName = tempFile.getName();
                        System.out.println("fileName = " + fileName);
                        //判断如果文件名包含敏感词汇就把这个文件复制出来
                        if(fileName.contains("毛泽东")){
                            source = new File(file2.getAbsolutePath());
                            //dest = new File("F:/文件存放/"+fileName);
                            dest = new File("F:/文件存放/毛泽东.docx");
                            System.out.println("source:"+source);
                            System.out.println("dest:"+dest);
                            copyFileUsingJava7Files(source, dest);
                        }
                        
                    }
                }
            }
        } else {
            System.out.println("文件不存在!");
        }
    }
    
    private static void copyFileUsingJava7Files(File source, File dest) throws Exception{
        java.nio.file.Files.copy(source.toPath(), dest.toPath());
    }
    
    public static void copyfilejia(File file, File toFile) throws Exception {
        byte[] b = new byte[1024];
        int a;
        FileInputStream fis;
        FileOutputStream fos;
        if (file.isDirectory()) {
            String filepath = file.getAbsolutePath();
            filepath=filepath.replaceAll("\\\\", "/");
            String toFilepath = toFile.getAbsolutePath();
            toFilepath=toFilepath.replaceAll("\\\\", "/");
            int lastIndexOf = filepath.lastIndexOf("/");
            toFilepath = toFilepath + filepath.substring(lastIndexOf ,filepath.length());
            File copy=new File(toFilepath);
            //复制文件夹
            if (!copy.exists()) {
                copy.mkdir();
            }
            //遍历文件夹
            for (File f : file.listFiles()) {
                copyfilejia(f, copy);
            }
        } else {
            if (toFile.isDirectory()) {
                String filepath = file.getAbsolutePath();
                filepath=filepath.replaceAll("\\\\", "/");
                String toFilepath = toFile.getAbsolutePath();
                toFilepath=toFilepath.replaceAll("\\\\", "/");
                int lastIndexOf = filepath.lastIndexOf("/");
                toFilepath = toFilepath + filepath.substring(lastIndexOf ,filepath.length());
                
                //写文件
                File newFile = new File(toFilepath);
                fis = new FileInputStream(file);
                fos = new FileOutputStream(newFile);
                while ((a = fis.read(b)) != -1) {
                    fos.write(b, 0, a);
                }
            } else {
                //写文件
                fis = new FileInputStream(file);
                fos = new FileOutputStream(toFile);
                while ((a = fis.read(b)) != -1) {
                    fos.write(b, 0, a);
                }
            }
 
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值