lines = FileUtils.readFile(filePath);
257 | for(int i = 0;i < lines.size();i++){// 将代码一行行写入Word中
258 | run = paragraph.createRun();// 创建段落文本
259 | run.setText(lines.get(i));// 设置段落文本
260 | if(i < lines.size() - 1){// 最后一行不用换行:防止两个源码文件间出现空行
261 | run.addBreak();// 设置换行
262 | }
263 | totalLines++;// 代码行计数
264 | if(lines.get(i).length() > 125){// 当一行代码的长度超过125时,应该会发生换行,一行代码在Word中可能会变成两行甚至更多行
265 | totalLines++;// 代码自动换行计数
266 | }
267 | }
268 | }
269 |
270 | /**
271 | * Word保存到本地
272 | * @param doc
273 | */
274 | public void saveDocx(XWPFDocument doc, String savePath){
275 | // 创建文件输出流:保存Word到本地
276 | try {
277 | FileOutputStream fout = new FileOutputStream(savePath);
278 | doc.write(fout);
279 | fout.close();
280 | } catch (FileNotFoundException e) {
281 | LogUtils.error("保存Word文档到本地时发生错误:" + e.getMessage());
282 | } catch (IOException e) {
283 | LogUtils.error("保存Word文档到本地时发生错误:" + e.getMessage());
284 | }
285 | }
286 |
287 | }
288 |
--------------------------------------------------------------------------------
/src/main/java/cocoas/FileUtils.java:
--------------------------------------------------------------------------------
1 | package cocoas;
2 |
3 | import java.io.*;
4 | import java.util.ArrayList;
5 | import java.util.Arrays;
6 | import java.util.List;
7 |
8 | /**
9 | * 描述:文件操作帮助类
10 | * 作者:蒋庆意
11 | * 日期时间:2021/1/18 11:27
12 | *
13 | * cocoasjiang@foxmail.com
14 | */
15 | public class FileUtils {
16 |
17 | /**
18 | * 获取文件名
19 | * @param filePath 文件路径
20 | * @return
21 | */
22 | public static String getFileName(String filePath){
23 | if(null == filePath || new File(filePath).isDirectory()){
24 | return "";
25 | }
26 | File file = new File(filePath);
27 | return file.getName();
28 | }
29 |
30 | /**
31 | * 判断文件是否符合指定的文件类型
32 | * @param f
33 | * @param fileTypes
34 | * @return
35 | */
36 | public static boolean matchFile(File f, List fileTypes){
37 | if(f == null || f.isDirectory()){
38 | return false;
39 | }
40 | for(String fileType : fileTypes){
41 | if(f.getAbsolutePath().endsWith(fileType)){
42 | return true;
43 | }
44 | }
45 | return false;
46 | }
47 |
48 |
49 | /**
50 | * 按行读取文件内容:过滤空行和注释
51 | * @param filePath
52 | * @return
53 | */
54 | public static List readFile(String filePath){
55 | if(null == filePath || new File(filePath).isDirectory()){
56 | return new ArrayList<>();
57 | }
58 | List lines = new ArrayList<>();
59 | try {
60 | BufferedReader reader = new BufferedReader(new FileReader(new File(filePath)));
61 | String line = reader.readLine();
62 | while(null != line){
63 | if(filterLine(line)){// 过滤不符合要求的代码行
64 | lines.add(line);
65 | }
66 | line = reader.readLine();
67 | }
68 | return lines;
69 | } catch (FileNotFoundException e) {
70 | LogUtils.error("读取文件<" + filePath + ">出错:" + e.getMessage());
71 | } catch (IOException e) {
72 | LogUtils.error("读取文件<" + filePath + ">出错:" + e.getMessage());
73 | }
74 | return new ArrayList<>();
75 | }
76 |
77 | /**
78 | * 过滤不符合要求的代码行
79 | * @param line
80 | * @return
81 | */
82 | public static boolean filterLine(String line){
83 | // 过滤空行
84 | if(null == line || line.trim().length() == 0){
85 | return false;
86 | }
87 | String lineTrim = line.trim();
88 | // 过滤一般的行注释//、块注释/* */、文档注释/** */
89 | if(lineTrim.startsWith("//") || lineTrim.startsWith("/*")
90 | || lineTrim.startsWith("*")){
91 | return false;
92 | }
93 | // 过滤html、xml注释:
94 | if(lineTrim.startsWith("