修改txt文档小说的标题格式

由于手机小说阅读器只识别“第xxx章”之类的这种标题,
故需将txt文档中标题中的“d{2,3}.?”修改为“第d{2,3}章”。
例:“01.萨卡斯基中将需要怀疑人生”修改为“第01章 萨卡斯基中将需要怀疑人生”
整体思路为:读取文档内容、regex替换文档相关内容、写入文档内容。
优化前代码如下:
 1 import java.io.*;
 2 import java.util.regex.Matcher;
 3 import java.util.regex.Pattern;
 4 
 5 public class Test {
 6     private static String read(File src){
 7         String text = "";
 8         if(!src.exists())
 9             return text;
10         StringBuilder sb = new StringBuilder(text);
11         BufferedReader br = null;
12         try {
13             br=new BufferedReader(new InputStreamReader(new FileInputStream(src),"gbk"));
14             String line;
15             while((line=br.readLine())!=null)
16                 sb.append(line).append("
");
17             text = new String(sb);
18         } catch (IOException e) {
19             e.printStackTrace();
20         } finally {
21             try {
22                 br.close();
23             } catch (IOException e) {
24                 e.printStackTrace();
25             }
26         }
27         return text;
28     }
29 
30     private static void write(File dest,String text){
31         BufferedWriter bw = null;
32         try {
33             if(!dest.exists())
34                 dest.createNewFile();
35             bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest),"utf8"));
36             bw.write(text);
37         } catch (IOException e) {
38             e.printStackTrace();
39         } finally {
40             try {
41                 bw.close();
42             } catch (IOException e) {
43                 e.printStackTrace();
44             }
45         }
46     }
47 
48     /**
49      * 将标题中的“d{2,3}.{0,1}”修改为“第d{2,3}章”
50      * @param src
51      * @param dest
52      */
53     public static void improveAndCopy(File src,File dest){
54         String text=read(src);
55         Pattern pattern = Pattern.compile("(\r\n)(\d{2,3})\.?");
56         Matcher m = pattern.matcher(text);
57         StringBuffer sb = new StringBuffer();
58         while(m.find())
59         /**
60          * 从上一次修改位置的下个字符开始,到这次修改位置,都append到sb中。
61          * 若为第一次修改,默认上一次修改位置为第0个字符。即从第一个字符到这次修改位置append到sb中。
62          */
63             m.appendReplacement(sb,m.group(1)+"第"+m.group(2)+"章 ");
64         //将文档中剩下的内容append到sb中
65         m.appendTail(sb);
66         text=sb.toString();
67         write(dest,text);
68     }
69 
70     public static void main(String[] args) {
71         File src =new File("C:\Users\Administrator\Downloads\我萨卡斯基才不会轻易狗带.txt");
72         File dest=new File("C:\Users\Administrator\Desktop\我萨卡斯基才不会轻易狗带.txt");
73         improveAndCopy(src,dest);
74     }
75 }

以上代码流程为:读取整片整篇文档、regex替换整篇文档、写入整篇文档。其中每次regex替换都需要扫描整篇文档。

文本量越大,regex替换所需时间越长。所以可以每读取一行,regex替换后直接写入,这样每次regex替换只需扫描一行内容(每行结尾为 ),从而减少regex替换所需时间。

优化后代码如下:

 1 import java.io.*;
 2 import java.util.regex.Matcher;
 3 import java.util.regex.Pattern;
 4 
 5 public class Test {
 6 
 7     /**
 8      * 将标题中的“d{2,3}.{0,1}”修改为“第d{2,3}章”
 9      * @param src
10      * @param dest
11      */
12     public static void improveAndCopy(File src,File dest){
13         BufferedReader br = null;
14         BufferedWriter wr = null;
15         try {
16             if(!src.exists())
17                 return;
18             if(!dest.exists())
19                 dest.createNewFile();
20             br=new BufferedReader(new InputStreamReader(new FileInputStream(src),"gbk"));
21             wr=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest),"utf8"));
22             String line;
23             while((line=br.readLine())!=null){
24                 line = replace(line);
25                 wr.write(line);
26                 wr.newLine(); //换行号
27             }
28             wr.flush(); //将缓冲区内容写入文件
29         } catch (IOException e) {
30             e.printStackTrace();
31         } finally {
32             try {
33                 br.close();
34                 wr.close();
35             } catch (IOException e) {
36                 e.printStackTrace();
37             }
38         }
39     }
40 
41     private static String replace(String content){
42         Pattern pattern = Pattern.compile("^(\d{2,3})\.?");
43         Matcher m = pattern.matcher(content);
44         if(m.find())
45             content = m.replaceFirst("第"+m.group(1)+"章 ");
46         return content;
47     }
48 
49     public static void main(String[] args) {
50         File src =new File("C:\Users\Administrator\Downloads\我萨卡斯基才不会轻易狗带.txt");
51         File dest=new File("C:\Users\Administrator\Desktop\我萨卡斯基才不会轻易狗带.txt");
52         improveAndCopy(src,dest);
53     }
54 }
原文地址:https://www.cnblogs.com/xiehuazhen/p/12558111.html