修改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("\r\n"); 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替換只需掃描一行內容(每行結尾為\r\n),從而減少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 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM