package test1;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* Created by 本地账户登录-xiaoCong on 2017/1/2.
*/
public class FileSearch {
// out/production/Java/test1/Test1.class
// E:\IntelliJ\Java\out\production\Java\test1
// public static String fileName = "";
// public static String srcPath = "";
// public static String desPath = "";
//public static String fileName = "Test1.class";
public static String fileName = "HasStatic.class";
// public static String fileName = "Test1.class";
public static String srcPath = "E:\IntelliJ\Java\out\production\Java";
//todo 注意,使用中最后的斜杠一定要,不然会跟文件名拼在一起
public static String desPath = "E:\IntelliJ\Java\out\production\Java\sort\";
public static void main(String[] args) {
FileSearch fileSearch = new FileSearch();
//List<File> fileList = fs.searchFile("a.jpg", "E:\huaxin\fivechess");
//List<File> fileList = fileSearch.searchFile(fileName, srcPath);
List<File> fileList = fileSearch.searchFile(fileName, srcPath );
if (0!=fileList.size()) {
for (File file : fileList ) {
fileSearch.cutFileTo(file,desPath);
}
}
}
public void cutFileTo(File file, String desPath) {
//this.cutFile(file, new File(desPath + fileName));
//this.cutFile(file, new File(desPath + fileName+new Date()));
//SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:ss:ss");
// UUID uuid = UUID.randomUUID();
// this.cutFile(file, new File(desPath+uuid.toString()+fileName));
String strFile = file.toString();
this.cutFile(file, new File(desPath + strFile.substring(strFile.lastIndexOf("\"))));
}
//todo 剪切功能没实现
public void cutFile(File srcFile, File file2) {
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
byte[] bytes = new byte[1024];
int temp = 0;
try {
inputStream = new FileInputStream(srcFile);
fileOutputStream = new FileOutputStream(file2);
while ((temp = inputStream.read(bytes)) != -1) {
fileOutputStream.write(bytes, 0, temp);
fileOutputStream.flush();
}
//todo 这里应该删除原来的,srcFile 没删除成功
srcFile.delete();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public List<File> searchFile(String strFileName, String path) {
return this.searchFile(fileName, srcPath , new ArrayList<File>() );
}
//"Chess.java"
public List<File> searchFile(String strFileName, String path, List<File> fileList ) {
// List<File> fileList = new ArrayList<>();
// 关联文件对象
File file = new File(path);
// 判断当前file是文件还是文件夹,是文件就输出文件绝对地址,是路径就继续查找
//todo 判断是否是文件夹
if (file.isFile()) {//是文件,或者用 !file.isDirectory()
//if (file.getName().equals(strFileName)) { //判断查出的文件名和初始的文件名是否相同,相同就打印,不同就往下一级继续查询
//模糊匹配
if (file.getName().contains(strFileName)) {
//将符合条件的地址打印在JTextArea中
//todo
fileList.add(file);
// this.cutFilesTo(file,desPath);
// System.out.println(file.getAbsolutePath());
}
} else {//是文件夹,递归
File[] files = file.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
// 将其中的文件夹遍历出来,并调用searchFile方法的调用
File childFile = files[i];
searchFile( strFileName,childFile.getAbsolutePath(),fileList);//这里用递归的思想,在方法中再调这个方法,完成在下一级目录的查询,一直到找出所有符合条件的文件
}
}
}
return fileList;
}
}