导航

将斗破苍穹按章分隔

Posted on 2013-07-08 16:27  zhustar  阅读(146)  评论(0编辑  收藏  举报

最近在网上看斗破苍穹,广告好多,于是下载了txt,太长?囧。所以想把它按章拆分,弄成网上阅读的样子。

txt的内容如下:

第001章 陨落的天才

“斗之力,三段!”

。。。

第002章 斗气大陆

月如银盘,漫天繁星。

。。。

。。。

用Java写了一段代码:

public static void main(String[] args) throws Exception {
        
        File file = new File("斗破苍穹utf-8.txt");
        Reader reader = null;
        reader = new FileReader(file);
        BufferedReader in = new BufferedReader(reader);
        
        Pattern pattern = Pattern.compile("第" + "[0-9]+" + "章");

        String s;

        File fp = new File("temp.txt");
        Writer currentWriter = new FileWriter(fp, true);    
        
        while ((s = in.readLine()) != null)
        {
            
            Matcher matcher = pattern.matcher(s);
            
            if (matcher.find())
            {
                currentWriter.close();
                
                //There are some title with ? sign.
                if(s.contains("?")){
                    s = s.replaceAll("[?]+", "");
                }
                
                String fileName = "output"+File.separator + s;
                File f = new File(fileName);
                System.out.println("Writing to file: " + s);
                currentWriter = new FileWriter(f, true);
                currentWriter.write(s);
                currentWriter.write("\n");
                currentWriter.flush();
            }else{
                currentWriter.write(s);
                currentWriter.write("\n");
                currentWriter.flush();
            }
        }        
        in.close();        
    }

 

因为处理的是中文,为确保中文能够正常显示,需要在eclipse中,Window->Preferences->General->Workspace,将Text fie encoding 改为UTF-8,同时将输入文件也改为UTF-8格式,(用notepad打开,另存为,然后选择格式为UTF-8)。

用该程序处理完之后,一个txt文件便根据里面的章节被分隔成了1647章。

这些文档就可以作为后续网站阅读的准备材料了。