package FictionManagementsSystem;
import java.io.*;
import java.util.*;
import org.apache.log4j.Logger;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class JQNovelTool {
public static Logger logger = Logger.getLogger(The_test.class);
private static String savePath = "D:/novels";
private static String serializePath = "D://novels//serialize.bin";
private static List<Novel> novelsList = new ArrayList<Novel>();
static {
File file = new File(savePath);
if (!file.exists()) {
file.mkdirs();
}
file = new File(serializePath);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 上传小说
*/
@SuppressWarnings("resource")
public static boolean upload() throws IOException {
String novelName;
String novelAuthor;
String uploadPath;
System.out.println("请输入上传小说名:");
novelName = new Scanner(System.in).nextLine();
logger.debug("用户输入上传小说名:" + novelName);
System.out.println("请输入上传小说作者:");
novelAuthor = new Scanner(System.in).nextLine();
logger.debug("用户输入上传小说作者:" + novelAuthor);
System.out.println("请输入上传小说路径:");
uploadPath = new Scanner(System.in).nextLine();
logger.debug("用户输入上传小说路径:" + uploadPath);
Novel novel = new Novel(novelsList.size(), novelName, novelAuthor);
if (uploadFile(novel, uploadPath)) {
novelsList.add(novel);
return true;
}
return false;
}
/**
* 显示所有小说信息
*/
public static void showNovels() {
getAllNovels();
}
/**
* 删除小说
*/
public static boolean remove() {
// 1.显示当前小说列表
getAllNovels();
// 2.获取用户输入编号
@SuppressWarnings("resource")
int sel = new Scanner(System.in).nextInt();
logger.debug("用户输入要删除的小说编号:" + sel);
if (sel < 0 || sel >= novelsList.size())
return false;
// 3.查找对应编号小说
Novel novel = getNovel(sel);
// 4.删除小说
new File(novel.getUploadPath()).delete();
return novelsList.remove(novel);
}
/**
* 下载小说到用户指定路径
*/
public static boolean download() throws IOException {
// 1.显示当前小说列表
getAllNovels();
// 2.获取用户输入编号
@SuppressWarnings("resource")
int sel = new Scanner(System.in).nextInt();
logger.debug("用户输入要下载的小说编号:" + sel);
// 3.判断是不中存在该文件
Novel novel = getNovel(sel);
if (novel == null)
return false;
// 2.获取用户输入的目标路径
System.out.println("请输入目标路径");
@SuppressWarnings("resource")
String path = new Scanner(System.in).nextLine();
logger.debug("用户输入要下载的目标路径:" + path);
return downloadFile(novel, path);
}
/**
* 读取小说
*/
public static boolean read() throws IOException {
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
// 1.显示当前小说列表
getAllNovels();
// 2.获取用户输入编号
@SuppressWarnings("resource")
String input = sc.nextLine();
logger.debug("用户输入小说编号:" + input);
if (!input.matches("\\d")) {
return false;
}
int sel = Integer.parseInt(input);
Novel novel = getNovel(sel);
if (novel == null)
return false;
read(novel);
return true;
}
/**
* 序列化
*/
public static void serialize() throws IOException {
File file = new File(serializePath);
FileOutputStream outStream = new FileOutputStream(file);
ObjectOutputStream objOutStream = new ObjectOutputStream(outStream);
objOutStream.writeObject(novelsList);
objOutStream.close();
}
/**
* 与反序列化
*/
public static void deserialize() throws IOException, ClassNotFoundException {
/*
* File file = new File(serializePath); if (file.length() <= 0) return;
* FileInputStream inStream = new FileInputStream(file); try { ObjectInputStream
* objInStream = new ObjectInputStream(inStream);
*
* @SuppressWarnings("unchecked") List<Novel> object = (ArrayList<Novel>)
* objInStream.readObject(); novelsList = object; } catch (Exception e) {
* e.printStackTrace(); }
*/
}
/**
* 从指定路径上传文件到数据库
*/
private static boolean uploadFile(Novel novel, String oriPath) throws IOException {
// 1.判断目标路径是否存在
File oriFile = new File(oriPath);
if (!oriFile.exists()) {
return false;
}
File tarFile = new File(savePath + File.separator + novel.getName() + ".txt");
BufferedReader reader = new BufferedReader(new FileReader(oriFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tarFile));
// 3.创建文件
String line = "";
while ((line = reader.readLine()) != null) {
writer.write(line);
}
// 4.给novel设置最终存储路径
novel.setUploadPath(tarFile.getAbsolutePath());
// 5.关闭流
reader.close();
writer.close();
return true;
}
/**
* 删除文件
*/
@SuppressWarnings("unused")
private static boolean deleteFile(String path) {
File file = new File(path);
if (file.exists()) {
file.delete();
return true;
}
return false;
}
/**
* 下载小说文件
*/
private static boolean downloadFile(Novel novel, String desPath) throws IOException {
// 1.判断目标文件是否存在且不是文件
File desfile = new File(desPath);
if (!desfile.exists() || desfile.isFile()) {
return false;
}
// 2.得到数据库中小说且创建文件流
File oriFile = new File(novel.getUploadPath());
BufferedReader reader = new BufferedReader(new FileReader(oriFile));
// 3.设置目标位置且创建文件流
BufferedWriter writer = new BufferedWriter(new FileWriter(desfile + File.separator + novel.getName() + ".txt"));
String line = "";
while ((line = reader.readLine()) != null) {
writer.write(line);
}
// 4.关闭通道
reader.close();
writer.close();
return true;
}
/**
* 通过索取获取list中的novel对象,没有则返回null
*/
private static Novel getNovel(int index) {
Iterator<Novel> it = novelsList.iterator();
while (it.hasNext()) {
Novel novel = it.next();
if (novel.getId() == index) {
return novel;
}
}
return null;
}
/**
* 分页读取小说内容
*/
private static void read(Novel novel) throws IOException {
int curRow = 0;// 当前阅读所在的页数
int count = getCharCount(novel);// 获取所有字符个数
int rowCount = 0;// 可以分为多少页
if (count % 100 == 0) {
rowCount = count / 100;
} else {
rowCount = count / 100 + 1;
}
// 传入指定页码,获取对应的100个字符
System.out.println(getReadStr(novel, curRow));
// 阅读选项
while (true) {
/*
* 语音阅读小说
*/
ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
Dispatch sapo = sap.getObject();
try {
// 音量 0-100
sap.setProperty("Volume", new Variant(100));
// 语音朗读速度 -10 到 +10
sap.setProperty("Rate", new Variant(2));
// 执行朗读
Dispatch.call(sapo, "Speak", new Variant(getReadStr(novel, curRow)));
} catch (Exception e) {
e.printStackTrace();
} finally {
sapo.safeRelease();
sap.safeRelease();
}
System.out.println("1.首页 2.上一页 3.下一页 4.尾页 5.退出阅读");
@SuppressWarnings("resource")
String input = new Scanner(System.in).nextLine();
if (!input.matches("\\d")) {
System.out.println("请输入对应选项");
continue;
}
logger.debug("用户输入阅读选项的对应选项:" + input);
int sel = Integer.parseInt(input);
switch (sel) {
case 1:
curRow = 0;
break;
case 2:
curRow -= 1;
if (curRow < 0) { // 不能让其越界
curRow = 0;
System.out.println("已到首页");
}
break;
case 3:
curRow += 1;
if (curRow >= rowCount) { // 不能让其越界
curRow = rowCount - 1;
System.out.println("已到尾页");
}
break;
case 4:
curRow = rowCount - 1;
break;
case 5:
return;
default:
System.out.println("没有该项操作");
}
System.out.println(getReadStr(novel, curRow));
}
}
/**
* 根据小说的路径,获取文件的字符个数(字符可以是英文,也可以是中文)
*/
private static int getCharCount(Novel novel) throws IOException {
File oriFile = new File(novel.getUploadPath());
FileReader fileReader = new FileReader(oriFile);
int count = 0;
// 通过遍历文件内容的方式来获取字符个数
while (fileReader.read() != -1) {
count++;
}
fileReader.close();
return count;
}
/**
* 读取给定小说的页码内容
*/
private static String getReadStr(Novel novel, int curRow) throws IOException {
// 1.取出小说对应的绝对路径
File oriFile = new File(novel.getUploadPath());
FileReader fileReader = new FileReader(oriFile);
fileReader.skip(curRow * 100); // 关键部分,使用skip函数
// 2.每次读取100个字符
char buf[] = new char[100];
fileReader.read(buf);
fileReader.close();
// 3.返回buf
return new String(buf);
}
/* 如果最后的内容不够100个字符也不会有问题,该read(buf)只是尽力的去填满这100的容量,不够就把剩余的内容装进去就好 */
/**
* 获取list中所有小说
*/
private static void getAllNovels() {
System.out.println("小说编号\t小说名称\t小说作者");
Iterator<Novel> it = novelsList.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
package FictionManagementsSystem;
import java.io.*;
public class Novel implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String name;
private String author;
private String uploadPath;
public Novel(int id, String name, String author) {
this.id = id;
this.name = name;
this.author = author;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public String getUploadPath() {
return uploadPath;
}
public void setUploadPath(String uploadPath) {
this.uploadPath = uploadPath;
}
@Override
public String toString() {
return this.getId() + "\t" + this.getName() + "\t" + this.getAuthor();
}
}
package FictionManagementsSystem;
import java.util.Scanner;
import org.apache.log4j.Logger;
public class The_test {
public static Logger logger = Logger.getLogger(The_test.class);
public static void main(String[] args) throws IOException, ClassNotFoundException {
while (true) {
System.out.println("欢迎使用Java小说阅读器");
System.out.println("请选择菜单:");
System.out.println("1.上传小说\t2.查看所有小说\t3.删除小说\t4.下载小说\t5.阅读小说");
// 1.反序列化
JQNovelTool.deserialize();
@SuppressWarnings("resource")
String input = new Scanner(System.in).nextLine();
if (!input.matches("\\d")) {
System.out.println("请输入对应选项");
continue;
}
int sel = Integer.parseInt(input);
logger.debug("用户输入:" + input);
switch (sel) {
case 1: {
if (JQNovelTool.upload()) {
System.out.println("上传成功");
// 序列化
JQNovelTool.serialize();
} else
System.out.println("上传失败");
}
break;
case 2: {
System.out.println("已上传的小说");
JQNovelTool.showNovels();
}
break;
case 3: {
System.out.println("请输入您要删除的小说编号");
if (JQNovelTool.remove()) {
System.out.println("删除成功");
// 序列化
JQNovelTool.serialize();
} else {
System.out.println("没有对应小说编号");
}
}
break;
case 4: {
System.out.println("请输入您要下载的小说编号");
if (JQNovelTool.download())
System.out.println("下载成功!");
else
System.out.println("没有对应小说编号或目录");
}
break;
case 5: {
System.out.println("请输入您要阅读的小说编号");
if (!JQNovelTool.read())
System.out.println("没有对应小说编号");
}
break;
default:
System.out.println("暂时没有对应的功能,程序员小哥哥正在开发中,敬请期待!");
break;
}
}
}
}