博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java读取txt文件,提取目录为html文件
阅读量:5772 次
发布时间:2019-06-18

本文共 3692 字,大约阅读时间需要 12 分钟。

hot3.png

(文中的代码源于链接:http://blog.csdn.net/inowcome/article/details/6047661,但自己运行时稍有问题,做了修改。)

代码功能:java读取txt文件的小应用,将txt文件里面的章节信息提取出来,生成一个动态链接的html文档。该代码源于网络,自己加以修改。给出核心的内容:

     

/*
 * 用来将只放在一个文件中的小说切隔成
 * 一个章节的html页面,其中每个页面还包括上一页,下一页,目录 的超链接
 * 同时还生成一个目录文件contents.html
 *====================
 *注意源码的编码是:utf-8
 *文件读取的来源文件的编码也需是utf-8
 *===================
 *这里的神墓的切隔的原理是:
 *根据第一个章节的标题都包含:"章 "
 *
 */
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class GeneraeHtml {
    private ArrayList<String> fileNames;
   
    public  GeneraeHtml()
    {
        fileNames=new ArrayList<String>();
    }
    public void generateHtmlByFile(File file)throws Exception
    {
            generate(file);
            generateContent();
       
    }
   
    //Create all chapters's html file
    private void generate(File file)throws Exception
    {
        String encoding = "GBK";
        boolean isFirstTitle=true;
        //Scanner sca=new Scanner(file);
        Scanner sca = new Scanner(new InputStreamReader(
                new FileInputStream(new File("e:/novel.txt")), encoding));
        String currentContent="";
        String currentLineStr="";
        String currentPageFileName="";
        String nextPageFileName="";
        int currentPageIndex=-1;
       
        //sca.useDelimiter("/n");
       
        //while(sca.hasNext())
        while(sca.hasNextLine())
        {
            currentLineStr=sca.nextLine();
            System.out.println(currentLineStr);
            if(currentLineStr.indexOf("章 ")!=-1)
            {
               
                if(!isFirstTitle)
                {
                    System.out.println("Current output title:"+currentPageFileName);
                    nextPageFileName=(currentPageIndex+1)+currentLineStr.trim()+".html";
                    fileNames.add(nextPageFileName);
                   
                    writeContent(currentContent,currentPageFileName,currentPageIndex);
                   
                    currentPageFileName=nextPageFileName;
                    currentContent="";
                }
                else
                {
                    currentPageFileName=(currentPageIndex+1)+currentLineStr.trim()+".html";
                    fileNames.add(currentPageFileName);
                    isFirstTitle=false;
                }
                currentPageIndex++;
               
            }
            currentContent+=currentLineStr+"</br>";
        }
        System.out.println(currentPageIndex);
        sca.close();
    }
   
    //It will write the current chapter into a html file
    private void writeContent(String bodyContent,String currentFileName,int currentPageIndex)throws Exception
    {
         int previousPageIndex=0;
         int nextPageIndex=currentPageIndex+1;
         if(currentPageIndex!=0)
         {
             previousPageIndex=currentPageIndex-1;
         }
         String pageContent="<html>/n<head>/n"
                             +"<meta http-equiv='content-type' content='text/html;charset=utf-8'>/n"
                             +"</head>/n<body bgcolor='#e6f3ff'>/n"
                             +bodyContent
                             +"</br>"
                             +"<table align='center'>"
                             +"<tr>"
                             +"<td><a href='./"+fileNames.get(previousPageIndex)+"'>上一页</a></td>"
                             +"<td><a href='./contents.html'>目录</a></td>"
                             +"<td><a href='./"+fileNames.get(nextPageIndex)+"'>下一页</a></td>"
                             +"</tr>"
                             +"</table>"
                             +"</body>/n</html>";
         String filePath="神墓/"+currentFileName;
         PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter(filePath)));
         out.print(pageContent);
         out.flush();
         out.close();
    }
   
    //Create a html file contain chapter's reference.
    private void generateContent()throws Exception
    {
        String pageContent="<html>/n<head>/n"
                            +"<meta http-equiv='content-type' content='text/html;charset=utf-8'>/n"
                            +"</head>/n<body bgcolor='#e6f3ff'>/n"
                            +"<table align='center' width='80%' border=1>"
                            +"<tr align='center'>";
        for(int i=0;i<fileNames.size();i++)
        {
            String item=fileNames.get(i);
            pageContent+="<td width=33% color='green'><a href='./"+item+"'>"+item+"</a></td>";
             if((i+1)%3==0)
            {
                pageContent+="</tr>/n<tr align='center'>";
            }
        }
         pageContent+="</table>/n</body>/n</html>";
         PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter("神墓/contents.html")));
         out.print(pageContent);
         out.flush();
         out.close();
                           
    }
   
    public static void main(String[] args) {
        GeneraeHtml generaeHtml=new GeneraeHtml();
        try
        {
           File file=new File("E:/novel.txt");
         
           generaeHtml.generateHtmlByFile(file);
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

          

转载于:https://my.oschina.net/u/232879/blog/155499

你可能感兴趣的文章
强化学习的未来— 第一部分
查看>>
TableStore:用户画像数据的存储和查询利器
查看>>
2019 DockerCon 大会即将召开,快来制定您的专属议程吧!
查看>>
15分钟构建超低成本数据大屏:DataV + DLA
查看>>
jSearch(聚搜) 1.0.0 终于来了
查看>>
盘点2018云计算市场,变化大于需求?
查看>>
极光推送(一)集成
查看>>
MySQL 8.0 压缩包版安装方法
查看>>
@Transient注解输出空间位置属性
查看>>
Ansible-playbook 条件判断when、pause(学习笔记二十三)
查看>>
5种你未必知道的JavaScript和CSS交互的方法(转发)
查看>>
线程进程间通信机制
查看>>
galera mysql 多主复制启动顺序及命令
查看>>
JS prototype 属性
查看>>
中位数性质——数列各个数到中位数的距离和最小
查看>>
WebApp之Meta标签
查看>>
添加Java文档注释
查看>>
Python3批量爬取网页图片
查看>>
iphone-common-codes-ccteam源代码 CCEncoding.m
查看>>
微信公众平台开发(96) 多个功能整合
查看>>