package tmp03;
/*
* 用来将只放在一个文件中的小说切隔成
* 一个章节的html页面,其中每个页面还包括上一页,下一页,目录 的超链接
* 同时还生成一个目录文件contents.html
*====================
*注意源码的编码是:utf-8
*文件读取的来源文件的编码也需是utf-8
*===================
*这里的神墓的切隔的原理是:
*根据第一个章节的标题都包含:"章 "
*
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import utils.FileUtils;
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 {
boolean isFirstTitle = true;
InputStreamReader isr = new InputStreamReader(
new FileInputStream(file), "GBK");
BufferedReader read = new BufferedReader(isr);
// BufferedReader source = new BufferedReader(new FileReader(file.getAbsolutePath()));
Scanner sca = new Scanner(read);
String currentContent = "";
String currentLineStr = "";
String currentPageFileName = "";
String nextPageFileName = "";
int currentPageIndex = -1;
sca.useDelimiter("/n");
while (sca.hasNextLine()) {
currentLineStr = sca.nextLine();
//正则表达式:我的小说章节名是以 ###开头,###结尾 ,所以用第二个更好
Pattern p = Pattern.compile("(第\\S*)[章节卷集部篇回](\\s)(\\S*)[^#]");
// Pattern p = Pattern.compile("(###)(.*)(###)");
Matcher matcher = p.matcher(currentLineStr);
if (matcher.find()) {
currentLineStr = matcher.group();
System.out.println("matcher.group() = " + matcher.group());
if (!isFirstTitle) {
System.out.println("Current output title:" + currentPageFileName);
nextPageFileName = (currentPageIndex + 1) + currentLineStr.trim() + ".html";
nextPageFileName= FileUtils.filterSpecialCharts(nextPageFileName, "");
fileNames.add(nextPageFileName);
// System.out.println("currentContent = " + currentContent);
writeContent(currentContent, currentPageFileName, currentPageIndex);
currentPageFileName = nextPageFileName;
currentContent = "";
} else {
currentPageFileName = (currentPageIndex + 1) + currentLineStr.trim() + ".html";
currentPageFileName = FileUtils.filterSpecialCharts(currentPageFileName, "");
fileNames.add(currentPageFileName);
isFirstTitle = false;
}
currentPageIndex++;
}
currentContent += currentLineStr + "</br>";
}
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 = outDirPath + FileUtils.filterSpecialCharts(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>";
String fileName = outDirPath + "contents.html";
System.out.println("pageContent = " + pageContent);
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
fileName)));
out.print(pageContent);
out.flush();
out.close();
}
public static final String filePath = "D:/06tmp/7175.txt";
public static final String outDirPath = "D:/06tmp/神墓/";
public static void main(String[] args) {
GeneraeHtml generaeHtml = new GeneraeHtml();
try {
File file = new File(filePath);
generaeHtml.generateHtmlByFile(file);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package tmp03;
/*
* 用来将只放在一个文件中的小说切隔成
* 一个章节的html页面,其中每个页面还包括上一页,下一页,目录 的超链接
* 同时还生成一个目录文件contents.html
*====================
*注意源码的编码是:utf-8
*文件读取的来源文件的编码也需是utf-8
*===================
*这里的神墓的切隔的原理是:
*根据第一个章节的标题都包含:"章 "
*
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import utils.FileUtils;
public class GeneraeHtml1 {
private ArrayList<String> fileNames;
public GeneraeHtml1() {
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 {
boolean isFirstTitle = true;
InputStreamReader isr = new InputStreamReader(
new FileInputStream(file), "GBK");
BufferedReader read = new BufferedReader(isr);
// BufferedReader source = new BufferedReader(new FileReader(file.getAbsolutePath()));
Scanner sca = new Scanner(read);
String currentContent = "";
String currentLineStr = "";
String currentPageFileName = "";
String nextPageFileName = "";
int currentPageIndex = -1;
sca.useDelimiter("/n");
while (sca.hasNextLine()) {
// currentLineStr = sca.next();
// System.out.println("currentLineStr = " + currentLineStr);
// System.out.println("currentLineStr 1= " + (currentLineStr = sca.nextLine()));
currentLineStr = sca.nextLine();
// System.out.println("currentPageIndex = " + (currentLineStr.indexOf("章") != -1));
if (currentLineStr.indexOf("章") != -1) {
if (!isFirstTitle) {
System.out.println("Current output title:" + currentPageFileName);
nextPageFileName = (currentPageIndex + 1) + currentLineStr.trim() + ".html";
nextPageFileName= FileUtils.filterSpecialCharts(nextPageFileName, "");
fileNames.add(nextPageFileName);
// System.out.println("currentContent = " + currentContent);
writeContent(currentContent, currentPageFileName, currentPageIndex);
currentPageFileName = nextPageFileName;
currentContent = "";
} else {
currentPageFileName = (currentPageIndex + 1) + currentLineStr.trim() + ".html";
currentPageFileName = FileUtils.filterSpecialCharts(currentPageFileName, "");
fileNames.add(currentPageFileName);
isFirstTitle = false;
}
currentPageIndex++;
}
currentContent += currentLineStr + "</br>";
}
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 = outDirPath + FileUtils.filterSpecialCharts(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>";
String fileName = outDirPath + "contents.html";
// File file = new File(fileName);
// file.createNewFile();
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
fileName)));
out.print(pageContent);
out.flush();
out.close();
}
public static final String filePath = "D:/06tmp/7175.txt";
public static final String outDirPath = "D:/06tmp/神墓/";
public static void main(String[] args) {
GeneraeHtml1 generaeHtml = new GeneraeHtml1();
try {
File file = new File(filePath);
generaeHtml.generateHtmlByFile(file);
} catch (Exception e) {
e.printStackTrace();
}
}
}
不咋好用
package tmp03;
/*
* 用来将只放在一个文件中的小说切隔成
* 一个章节的html页面,其中每个页面还包括上一页,下一页,目录 的超链接
* 同时还生成一个目录文件contents.html
*====================
*注意源码的编码是:utf-8
*文件读取的来源文件的编码也需是utf-8
*===================
*这里的神墓的切隔的原理是:
*根据第一个章节的标题都包含:"章 "
*
*/
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
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 {
boolean isFirstTitle = true;
Scanner sca = new Scanner(file);
String currentContent = "";
String currentLineStr = "";
String currentPageFileName = "";
String nextPageFileName = "";
int currentPageIndex = -1;
sca.useDelimiter("/n");
while (sca.hasNext()) {
currentLineStr = sca.next();
// System.out.println("currentPageIndex = " + currentPageIndex);
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>";
}
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>";
String fileName = "D:/06tmp/神墓/contents.html";
// File file = new File(fileName);
// file.createNewFile();
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
fileName)));
out.print(pageContent);
out.flush();
out.close();
}
public static void main(String[] args) {
GeneraeHtml generaeHtml = new GeneraeHtml();
try {
File file = new File("D:/06tmp/7175.txt");
generaeHtml.generateHtmlByFile(file);
} catch (Exception e) {
e.printStackTrace();
}
}
}