• 将斗破苍穹按章分隔


    最近在网上看斗破苍穹,广告好多,于是下载了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章。

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

  • 相关阅读:
    Semaphore
    财报分析
    关于C#中的new的用法
    Linux(CentOS)下Postgresql数据库的安装配置
    CentOS下实现SCP免输密码传送文件
    HiveQL逻辑执行顺序
    CentOS上以源码的方式安装Redis笔记
    Python学习心得(七) 深入理解threading多线程模块
    SQL Server返回两个Date日期相差共多少天零多少小时零多少分钟零多少秒
    Python学习心得(六) 反射机制、装饰器
  • 原文地址:https://www.cnblogs.com/stanzhu/p/3177182.html
Copyright © 2020-2023  润新知