📄 dbtofile.java
字号:
package cn.yang.novel.file;
import java.io.*;
import java.sql.*;
import java.util.*;
import cn.yang.novel.db.*;
import cn.yang.novel.entity.*;
import cn.yang.novel.data.FetchData;
public class DbToFile {
// 提供输入的目标文件
public void setNovelDir(String dstNovelDir) throws Exception {
// 一个数据库的连接
Db db = new Db();
Connection conn;
conn = db.getConnection();
Connection mysql = db.getMysqlConnect();
PreparedStatement stat = mysql
.prepareStatement("insert into ebookinfo(name,author,desc,class,docid) values(?,?,?,?,?);\r\n");
File distFile = new File(dstNovelDir);
if (!distFile.isDirectory()) {
distFile.mkdirs();
}
// System.out.println( stat.toString() );
File sqlfile = new File(distFile.toString() + "sql\\mysql.txt");
sqlfile.getParentFile().mkdirs();
// FileOutputStream os = ;//OutputStream os = new OutputStream()
OutputStreamWriter sqlStream = new OutputStreamWriter(
new FileOutputStream(sqlfile), "utf-8");
sqlStream.write("use ebook;\r\n");
distFile.mkdirs();
createNovels(0, sqlStream, distFile, conn, stat);
conn.close();
sqlStream.close();
}
// 从数据库中查找小说,每有一本小说就写成一个文件
public void createNovels(int start, OutputStreamWriter sqlStream,
File dstFile, Connection conn, PreparedStatement stat)
throws Exception {
// 设置小说的id号码
String novelContent_n = null;
// 产生一个操作数据库的对象
FetchData fetchData = new FetchData();
String sql = "SELECT ID, Title, PageCount, HasCount, DocID,"
+ "Type_flag, intro, author, class,Content_TableName FROM novelList"
+ " where Type_flag = 1 order by docid";
Statement statement = conn.createStatement();
// 产生小说的对象
ArrayList novels = fetchData.getNoveListsFromDatabase(statement.executeQuery(sql ));// 0,1000,2000,3000,
int docId;
File novelFile = null;
BufferedWriter fw;
// PreparedStatement novelchapterPreStatment = null;
// conn.c
// novelchapterPreStatment = conn.prepareStatement(sqlchapter);
int count = 0;
for (int i = 0; i < novels.size(); i++) {
docId = ((NoveList) novels.get(i)).getDocID();
try {
// 小说的标题号码
// int docId = novels.getDocID();
// 不关闭文件流
// isCloseFile = false;
count++;
if ((count % 10) == 0) {
System.out.println("write:" + count);
}
novelFile = new File(dstFile, docId + ".txt");
fw = new BufferedWriter(new FileWriter(novelFile));
novelContent_n = ((NoveList) novels.get(i))
.getContent_TableName();
// 写入简单的内容,并返回这个文件
// File novelFile =
// writeNameTOFile(dstFile,((NoveList)novels.get(i)) );
// writeNameTOFile(fw, ((NoveList) novels.get(i)));
// 得到小说章节
// con = db.getConnection();
// Statement statment = con.createStatement();
// novelchapterPreStatment.setInt(1, docId);
// System.out.println("Chapter:" +
// novelchapterPreStatment.toString());
String sqlchapter = "SELECT ID, DocID, Title, ChapterNo, PageFrom, PageTo, JumpUrl FROM NovelChapter where docId ="
+ docId + " order by ChapterNo";
String sqlcontent = "select pageno,Content from "
+ novelContent_n + " where docId=" + docId
+ " order by PageNo asc";
long t1 = System.currentTimeMillis();
ArrayList novelChapters = fetchData.getNovelChapter(statement
.executeQuery(sqlchapter));
Hashtable contentHash = fetchData.getNovelContent(statement
.executeQuery(sqlcontent));
long t2 = System.currentTimeMillis();
for (int j = 0; j < novelChapters.size(); j++) {
// 得到各个章节的全部
NovelChapter novelChapter = (NovelChapter) novelChapters
.get(j);
StringBuffer strBuff = new StringBuffer();
for (int pageno = novelChapter.getPageFrom(); pageno < novelChapter
.getPageTo(); pageno++) {
String content = (String) contentHash.get(new Integer(
pageno));
if (content == null || content.length() == 0)
continue;
if (strBuff.length() == 0
&& !content.startsWith(novelChapter.getTitle())) {
strBuff.append(novelChapter.getTitle());
strBuff.append("\n");
}
strBuff.append(content + "\n");
}
novelChapter.setContent(strBuff.toString());
writeContetToFile(fw, novelChapter);
}
stat.setString(1, ((NoveList) novels.get(i)).getTitle());
stat.setString(2, ((NoveList) novels.get(i)).getAuthor());
stat.setString(3, ((NoveList) novels.get(i)).getIntro());
stat.setString(4, ((NoveList) novels.get(i)).getBookClass());
stat.setInt(5, docId);
String sql_str = stat.toString();
sqlStream.write(sql_str.substring(sql_str.indexOf("insert")));
fw.close();
// long t3 = System.currentTimeMillis();
System.out.println("read db:" + (t2 - t1));
// System.out.println("t2-t3:分析写入文件" + (t3 - t2));
} catch (Exception ex) {
System.out.println(docId + ":" + ex.toString());
}
}
statement.close();
}
// 向文件写小说标题,作者,简介,类别的方法
public void writeNameTOFile(BufferedWriter fw, NoveList novelist) {
try {
// 小说标题
String title = novelist.getTitle();
// 小说作者
String author = novelist.getAuthor();
// 小说简介
String info = novelist.getIntro();
// 小说类别
String bookClass = novelist.getBookClass();
StringBuffer str = new StringBuffer();
str.append("小说标题:");
str.append(title);
str.append("\n");
str.append("小说作者:");
str.append(author);
str.append("\n");
str.append("小说简介:");
str.append(info);
str.append("\n");
str.append("小说类别:");
str.append(bookClass);
str.append("\n");
// str.append()
str.append("正文:");
str.append("\n");
fw.write(str.toString());
// fw.close();
} catch (Exception e) {
e.printStackTrace();
}
// return novelFile;
}
// 小文件写小说的章节内容
public void writeContetToFile(BufferedWriter fw, NovelChapter novelChapter) {
try {
// FileWriter fw = new FileWriter(novelFile,true);
// if ( novelChapter.getTitle())
// fw.write("\r\n");
// fw.write(novelChapter.getTitle() + "\r\n");
fw.write(novelChapter.getContent() + "\r\n");
// System.out.println(novelChapter.getTitle());
// if(isCloseFile)
// fw.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 测试使用
public static void main(String args) {
DbToFile dbTofile = new DbToFile();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -