?? 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 + -