main
黄海 9 months ago
parent 753a094175
commit 04a27808ba

@ -1,5 +1,6 @@
package com.dsideal.base.Test; package com.dsideal.base.Test;
import cn.hutool.core.io.FileUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFRow;
@ -15,20 +16,24 @@ import java.util.List;
public class ReadDoc { public class ReadDoc {
//https://blog.csdn.net/a346736962/article/details/123037797 //https://blog.csdn.net/a346736962/article/details/123037797
public static void main(String[] args) throws IOException, InvalidFormatException { public static void main(String[] args) throws IOException, InvalidFormatException {
String workingPath = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\16个州市报告2022\\分析报告20240510"; String directoryPath = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\16个州市报告2022\\分析报告20240510";
//遍历workingPath下的所有文件 directoryPath="D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\133个县区报告2022";
File file = new File(workingPath); //遍历workingPath下的所有文件,注意,需要递归所有子目录下的所有文件
String[] fileList = file.list(); System.out.println("开始遍历目录下的所有文件");
for (String fileName : fileList) { // 调用 Hutool 的 FileUtil.loopFiles 方法递归获取所有文件
//判断是否为文件夹 List<File> files = FileUtil.loopFiles(directoryPath, file -> {
File tempFile = new File(workingPath + "/" + fileName); // 这里可以添加你的过滤条件,如果不需要过滤,返回 true 即可
if (tempFile.isDirectory()) { return true;
continue; });
} int cnt = 0;
// 打印所有文件的路径
for (File file : files) {
String fileName = file.getName();
//判断是否为docx文件 //判断是否为docx文件
if (fileName.endsWith(".docx") && !fileName.startsWith("~")) { if (fileName.endsWith(".docx") && !fileName.startsWith("~")) {
cnt++;
//读取文件 //读取文件
String inputUrl = workingPath + "/" + fileName; String inputUrl = file.getAbsolutePath();
InputStream is = new FileInputStream(inputUrl); InputStream is = new FileInputStream(inputUrl);
XWPFDocument doc = new XWPFDocument(is); XWPFDocument doc = new XWPFDocument(is);
//图表 //图表
@ -67,6 +72,6 @@ public class ReadDoc {
} }
} }
} }
System.out.println("共读取" + cnt + "个文件");
} }
} }

Loading…
Cancel
Save