文本阅读代码

package com.ambow.test.read;


import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;


public class Separater {
	
	
	@Autowired
	private HttpServletRequest request;
	
/**
* 生成小说文件夹
* @return 成功生成返回文件夹名,已存在返回null
*/
private static String genarateFolder(File novel) {
	if(!novel.isFile() || !novel.getAbsolutePath().endsWith(".txt")) {
	return null;
	}
	String novelName = novel.getAbsolutePath();
	String folderName = novelName.substring(0, novelName.indexOf(".txt"));
	File folder = new File(folderName);
	if (!folder.exists()) {
	folder.mkdirs();
	return folderName;
	}
	return null;
}
    /**
     * 输出html文件
     * @param bodyContent
     * @param currentFileName
     * @param currentPageIndex
     * @throws Exception
     */
    private static void generateChapterHtmlFile(int currentPageIndex,String content,List<String> chapterList,String folderName)throws Exception {
         String pageContent="<html><head>"
                             +"<meta http-equiv='content-type' content='text/html;charset=utf-8'>"
                             +"<title>"+chapterList.get(currentPageIndex)+"</title>"
                             +"</head><body bgcolor='#eeeeee' id='body'>"
                             +"<h1 align='center'>"+chapterList.get(currentPageIndex)+"</h1>"
                             +"<div style='line-height:40px;font-size:14px;width: 50%;margin :auto'>"+content+"</div>"
                             +"</br>"
                             +"<table align='center'>"
                             +"<tr>"
                             +"<td><a href='"+chapterList.get(currentPageIndex==0?0:currentPageIndex-1)+".html'>上一页</a></td>"
                             +"<td><a href='contents.html'>目录</a></td>"
                             +"<td><a href='"+chapterList.get(currentPageIndex==chapterList.size()-1?chapterList.size()-1:currentPageIndex+1)+".html'>下一页</a></td>"
                             +"</tr>"
                             +"</table>"
                             +"</body></html>";
         String filePath=folderName+"\\"+chapterList.get(currentPageIndex)+".html";
         PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter(filePath)));
         out.print(pageContent);
         out.flush();
         out.close();
    }
    /**
     * 获取章节列表
     * @param novel
     * @throws Exception
     */
    private static List<String> getChapterList(File novel) throws Exception {
    List<String> chapterList = new ArrayList<String>();
    FileInputStream fileInputStream = new FileInputStream(novel);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,getCharsetOfNovel(novel));
BufferedReader novelbr = new BufferedReader(inputStreamReader);
int currentIndex = 1;
String line = novelbr.readLine();
while(line != null) {
if(line.indexOf("第") != -1 && line.indexOf("节") != -1) {
	System.out.println(line.toString());
	chapterList.add("第" + currentIndex + "章" + line.substring(line.indexOf("节")+1));
	System.out.println(chapterList.get(chapterList.size()-1));
	currentIndex ++;
} else if (line.indexOf("第") != -1 && line.indexOf("章") != -1) {
	System.out.println(line.toString());
	chapterList.add("第" + currentIndex + "章" + line.substring(line.indexOf("章")+1));
	System.out.println(chapterList.get(chapterList.size()-1));
	currentIndex ++;
}
line = novelbr.readLine();
}
novelbr.close();
fileInputStream.close();
return chapterList;
    }
    private static void generateChapterMenuHtmlFile(String folderName,List<String> chapterList)
    		throws Exception {
    String menuPath = folderName+"\\contents.html";
    StringBuilder pageContent = new StringBuilder();
    pageContent.append("<html><head>"
                +"<meta http-equiv='content-type' content='text/html;charset=utf-8'>"
                +"<title>"+folderName.substring(folderName.lastIndexOf("\\")+1)+"章节目录</title>"
                +"<head>"
                +"<body bgcolor='#e6f3ff' id='body'>"
                + "<h3 align='center'>章节目录</h3><br>"
                +"<a href='http://localhost:8888/ambowbook/index.action'>首页</a>"
                + "<table style='margin :auto;'  cellpadding='10px' cellspacing='0' align='center' border='1'>");
    for (int i = 0; i < chapterList.size(); i++) {
    if(i == 0) {
    pageContent.append("<tr>");
    } else if(i % 5 == 0) {
    pageContent.append("<td><a href='"+chapterList.get(i)+".html'>"+chapterList.get(i)+"</a></td>");
    pageContent.append("</tr>");
    }
pageContent.append("<td><a href='"+chapterList.get(i).trim()+".html'>"+chapterList.get(i)+"</a></td>");
}
    pageContent.append("</table></body></html>");
    PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter(menuPath)));
        out.print(pageContent.toString());
        out.flush();
        out.close();
    }
    /**
     * 判断TXT文件编码方式
     * @param fileName
     * @return
     * @throws IOException
     */
    private static String getCharsetOfNovel(File novel) throws IOException {  
    BufferedInputStream bin = new BufferedInputStream(new FileInputStream(novel));  
         byte[] head = new byte[3];  
         bin.read(head, 0, head.length);
         String encoding = "gb2312";    
         if (head[0] == -1 && head[1] == -2 )    
        encoding = "UTF-16";    
         if (head[0] == -2 && head[1] == -1 )    
        encoding = "Unicode";    
         if(head[0]==-17 && head[1]==-69 && head[2] ==-65)    
          encoding = "UTF-8";       
         return encoding;   
    } 
    public static void generate(File novel) throws Exception {
		    String folderName = genarateFolder(novel);
		    if(folderName == null) {
		    		return ;
		    }
    List<String> chapterList = getChapterList(novel);
    generateChapterMenuHtmlFile(folderName, chapterList);
    FileInputStream fileInputStream = new FileInputStream(novel);
			InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,getCharsetOfNovel(novel));
			BufferedReader novelbr = new BufferedReader(inputStreamReader);
			int currentPageIndex = -1;
			StringBuilder content = new StringBuilder();
			String line = novelbr.readLine();
			while(line != null) {
			if(line.indexOf("第") != -1 && (line.indexOf("节") != -1 || line.indexOf("章") != -1)) {
				if(currentPageIndex > -1) {
					generateChapterHtmlFile(currentPageIndex, content.toString(),chapterList,folderName);
					content.delete(0, content.length());
				}
				currentPageIndex ++;
				} else if(currentPageIndex > -1) {
					content.append(line+"<br>");
				}
			line = novelbr.readLine();
			}
			novelbr.close();
			fileInputStream.close();
    }
    public static void main(String[] args) throws Exception {
    	
	File folder = new File("TXT文件所在路径");
		Separater.generate(folder);
		System.out.println(folder.exists());
}

}
----------------------------------
@RequestMapping(value = "test.action")
	public String test(String bookname,HttpServletRequest request) {		
	try {
		File folder = new File(request.getSession().getServletContext().getRealPath("/")+"book/"+bookname+".txt");
		Separater.generate(folder);
		System.out.println(folder.exists());
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return  "downStory";
	}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值