java小说阅读器

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;
			}
		}
	}
}
  • 16
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
电子书阅读器是一种能够在电子设备上阅读电子书籍的应用程序。Java是一种面向对象的编程语言,可以通过使用Java开发工具和框架来实现电子书阅读器的功能。 在Java项目电子书阅读器的开发过程中,首先需要确定基本的功能需求。这包括电子书的导入、显示电子书内容、翻页、书签功能、目录管理、字体和背景设置等。可以通过Java的GUI库(如JavaFX或Swing)来创建用户界面,实现用户与电子书阅读器的交互。 在电子书的导入方面,可以使用Java的文件操作功能来读取电子书文件,如PDF或EPUB格式的文件。通过解析文件内容,将电子书的文本内容以适当的方式呈现给用户。 为了实现电子书的翻页功能,可以使用Java的图形库绘制书页,并通过事件监听器捕捉用户的翻页动作。进一步优化体验的话,可以预加载前后几页的内容,提高翻页的速度和流畅度。 为了提供更好的阅读体验,可以考虑为用户提供书签功能,使其能够标记关键页面或章节,并随时跳转到标记位置。同时,还可以实现目录管理功能,使用户能够快速查找和跳转至目录中的章节。 为了满足用户对个性化设置的需求,可以提供字体和背景设置等功能。用户可以根据自己的喜好,自定义电子书的阅读界面。 在项目开发过程中,可以使用开发工具(如Eclipse或IntelliJ IDEA)来编写、调试和测试代码。同时,可以使用版本控制工具(如Git)来管理代码的版本和协作开发。 总之,通过使用Java的编程技术和相关工具,可以实现一个功能丰富、用户友好的电子书阅读器应用程序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GBK_UTF-8

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值