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);
}
}
}
}
}