zoukankan      html  css  js  c++  java
  • 将斗破苍穹按章分隔

    最近在网上看斗破苍穹,广告好多,于是下载了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("
    ");
                    currentWriter.flush();
                }else{
                    currentWriter.write(s);
                    currentWriter.write("
    ");
                    currentWriter.flush();
                }
            }        
            in.close();        
        }

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

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

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

  • 相关阅读:
    面向对象的三个基本特征
    OGRE启动过程详解(OGRE HelloWorld程序原理解析)
    Bullet核心类介绍(Bullet 2.82 HelloWorld程序及其详解,附程序代码)
    windows下Bullet 2.82编译安装(Bullet Physics开发环境配置)
    1303: Decimal
    分组背包,每组最多选1个
    椒盐效果
    自我介绍
    题目1539:师弟
    upper_bound()
  • 原文地址:https://www.cnblogs.com/stanzhu/p/3177182.html
Copyright © 2011-2022 走看看