java 文件分类
首先 把 需要分类的 文件后缀 写上
配置分类后 保存到哪个文件夹
然后启动main方法即可
一个刚入行不久当得 程序员
程序如有bug 欢迎 各位大佬指正
就懒得打包exe 了
配置
https://cdn-131.myfile.is/q1w5J3R0ye/3f9bdd2a-1673703594/123.PNG
按照 文件 后缀 加 最后访问日期 分类
https://cdn-120.myfile.is/xdx5J4Rby8/5721b8a7-1673703779/1234.PNG
分类结果
https://cdn-115.myfile.is/h3y4JcR7y5/b92a1b26-1673703867/12345.PNG
public class Test {
private staticList<String> files = new ArrayList<String>();
private staticList<String> ends = new ArrayList<String>();
private static Integer count = 0;
private static String path = "D:\\File\\分类\\";
public static void main(String[] args) throws Exception {
ends.add("pdf");
ends.add("doc");
ends.add("docx");
ends.add("xls");
ends.add("xlsx");
ends.add("ppt");
ends.add("pptx");
ends.add("txt");
ends.stream().forEach(e->{
String dirStr = path+e.toString();
File directory = new File(dirStr);
if (!directory.exists()) {
directory.mkdirs();
}
});
// ends.add(".txt");
// ends.add(".rar");
// ends.add(".zip");
// ends.add(".7z");
// deleteChina("C:\\Users\\15400\\Documents");
// deleteChina("D:\\");
classifyFile();
}
public static void classifyFile(){
classifyFile(null);
FutureUtil.shutdown();
}
public static void classifyFile(String fileLocation) {
count++;
System.out.println(count);
File file;
if (!HuStringUtils.isEmpty(fileLocation)){
file = new File(fileLocation);
}else {
file = null;
File[] parts =File.listRoots();
Arrays.stream(parts).forEach(e->{
ArrayList<String> dir = Dir(new File(e.toString()));
dir.stream().forEach(f->{
classifyFile(f);
});
});
}
if (null!=file &&file.exists()) {
if (file.isDirectory() && !path.equals(fileLocation)) {
System.out.print("--文件夹");
System.out.println(fileLocation);
ArrayList<String> dir = Dir(new File(fileLocation));
FutureUtil.synchronizeExecute(2, TimeUnit.HOURS,()->{
System.out.println("执行"+Thread.currentThread().getName());
dir.stream().forEach(e->{
classifyFile(e);
});
return null;
});
} else {
System.out.print("--文件");
System.out.println(fileLocation);
ends.forEach(e->{
if (fileLocation.endsWith(e)){
file.getName();
String lastAccessTime = getLastAccessTime(file);
String pathName =path +e+"\\"+lastAccessTime.replaceAll("-","").substring(0,6)+"\\";
File pathFile = new File(pathName);
if (!pathFile.exists()) {
pathFile.mkdirs();
}
File copyFile = new File(pathName+ file.getName());
try {
copyFile(file, copyFile);
count++;
} catch (IOException ex) {
throw new RuntimeException(ex);
}
files.add(fileLocation);
System.out.println(Thread.currentThread().getName());
}
});
// Stream<String> of1 = StreamUtil.of(file, CharsetUtil.CHARSET_UTF_8);
// StringBuilder content = new StringBuilder();
//
// of1.forEachOrdered(e->{
// content.append(e.replaceAll(REGEX_CHINESE,"")).append("\n");
// });
//
// BufferedWriter out = null;
// try {
// out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
// String target = content.toString();
// out.write(target);
// out.flush();
// out.close();
// } catch (Exception e) {
// }finally {
// }
}
}
// return null;
}
private static void copyFile(File source, File dest) throws IOException {
if (!dest.exists()) {
FileChannel inputChannel = null;
FileChannel outputChannel = null;
try {
inputChannel = new FileInputStream(source).getChannel();
outputChannel = new FileOutputStream(dest).getChannel();
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
} finally {
inputChannel.close();
outputChannel.close();
}
}
}
public static ArrayList<String> Dir(File dirFile){
ArrayList<String> dirStrArr = new ArrayList<String>();
if (dirFile.exists()) {
//直接取出利用listFiles()把当前路径下的所有文件夹、文件存放到一个文件数组
File files[] = dirFile.listFiles();
if (null!=files ) {
for (File file : files) {
//如果传递过来的参数dirFile是以文件分隔符,也就是/或者\结尾,则如此构造
if (dirFile.getPath().endsWith(File.separator)) {
dirStrArr.add(dirFile.getPath() + file.getName());
} else {
//否则,如果没有文件分隔符,则补上一个文件分隔符,再加上文件名,才是路径
dirStrArr.add(dirFile.getPath() + File.separator + file.getName());
}
}
}
}
return dirStrArr;
}
public static void readeFile(File file) throws IOException {
if(file.exists()){
for(File fileson : file.listFiles()){
if(fileson.isFile()){
//对java文件逐行读取修改注释
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(fileson));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
StringBuilder content = new StringBuilder();
String tmp = null;
while ((tmp = reader.readLine()) != null) {
if(tmp.indexOf("//") >= 0){
tmp = tmp.replace(tmp.substring(tmp.indexOf("//"),tmp.length()),"");
}
//删除已空格开始,以空格结尾的行
if(tmp.matches("^\t+$")){
continue;
}
//删除空行
tmp = tmp.replaceAll("((\r\n)|\n)[\\s\t ]*(\\1)+", "$1");
if("".equals(tmp) || null == tmp || "\t".equals(tmp) || "".equals(tmp.trim())){
continue;
}
content.append(tmp);
content.append("\n");
}
String target = content.toString();
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileson)));
out.write(target);
out.flush();
out.close();
} else {
System.out.println(fileson.getName());
readeFile(fileson);
}
}
}
}
public static String getLastAccessTime(File file) {
if (file == null) {
return null;
}
BasicFileAttributes attr = null;
try {
Path path =file.toPath();
attr = Files.readAttributes(path, BasicFileAttributes.class);
} catch (IOException e) {
e.printStackTrace();
}
// 上次访问时间
Instant instant = attr.lastAccessTime().toInstant();
String format = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneId.systemDefault()).format(instant);
return format;
}
} private static String REGEX_CHINESE = "[\u4e00-\u9fa5]";
注释代码打开指定目录关掉 copy文件的 就一键去 中文注释 了{:1_911:}{:1_911:} 学习了很多 瞅瞅瞅瞅 谢谢楼主分享 FutureUtil 与 HuStringUtils 是工具类吗? luxingyu329 发表于 2023-1-15 02:53
FutureUtil 与 HuStringUtils 是工具类吗?
啊 这我写的 。我上班代码也放上去 感谢分析工具,先收藏,后面可能用得上 很实用学习到了{:1_893:} 工具类可以贴出来么
页:
[1]
2