(文中的代码源于链接: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(); } } }