package com.june;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Client {
public static Map<String, File> STU_FILES = new HashMap<String, File>();
public static List<String> STU_RS = new ArrayList<String>();
public static void main(String[] args) throws Exception {
findBooksByName("XX");
}
public static List<String> findBooksByName(String stuName) throws Exception {
STU_RS = new ArrayList<String>();
STU_FILES = new HashMap<String, File>();
String path = "D:/mySVN";
findAllFiles(new File(path), stuName);
dealFiles(stuName);
return STU_RS;
}
public static void findAllFiles(File f, String stuName) {
if (f.isFile()) {
if (f.getAbsolutePath().contains(stuName)) {
STU_FILES.put(toNewPath(f), f);
}
} else {
File[] fs = f.listFiles();
for (File tempFile : fs) {
findAllFiles(tempFile, stuName);
}
}
}
public static String toNewPath(File file) {
String fileName = file.getParent().substring(file.getParent().lastIndexOf("\\") + 1) + "-"
+ file.getName().substring(file.getName().lastIndexOf("_") + 1);
return fileName;
}
public static void dealFiles(String stuName) throws Exception {
// 1、构建文件的目录
String filePath = "d:/stuBooks/" + stuName + "/";
File dir = new File(filePath);
dir.mkdirs();
// 清除目录的文件
File[] fs = dir.listFiles();
for (File f : fs) {
f.delete();
}
Set<String> keys = STU_FILES.keySet();
Iterator<String> it = keys.iterator();
while (it.hasNext()) {
String key = it.next();
File tempFile = STU_FILES.get(key);
// 构建文件名
String fileName = toNewPath(tempFile);
String fileWholeName = filePath + fileName;
fileCopy(tempFile, new File(fileWholeName));
System.out.println(fileWholeName);
STU_RS.add(fileWholeName);
}
}
public static void fileCopy(File sourceFile, File targetFile) {
try {
Files.copy(sourceFile.toPath(), targetFile.toPath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
文件操作
最新推荐文章于 2024-02-29 09:07:03 发布