需求:实现下面功能

欢迎进入小说管理系统

请选择菜单

1.上传小说

2.查看所有小说

3.删除小说

4.下载小说

5.阅读小说

阅读小说时要实现分页功能,每页显示100个字符且有以下选项

1.首页 2.上一页 3.下一页 4.尾页 5.退出阅读

提示:

创建一个小说类(小说编号,小说名,作者,上传后的路径);用一个list来存储当前的小说数量;固定一个用户目录用来存储用户上传的小说;阅读功能可以用一个变量来标记当前页;

思路:

上传小说,就把用户指定的路径下的文件通过流复制到我们事先设定好的一个文件夹中

查看所有小说时,直接遍历list即可

通过遍历查找对应id的小说对象,然后从list中删除该对象,最后把该对象中存储的小说路径下的小说删除

下载小说可以从小说对象的路径中复制到用户指定目录

阅读分页需要知道小说字符个数,然后/100来编号,最后使用skip()函数来达到目的

给list存档,每次选择系统功能前都先要反序列化出来list;每次执行完上传,删除功能后都应该序列化list;

入口函数:Main函数

public class Main {

public static void main(String[] args) throws IOException, ClassNotFoundException {

while(true){

System.out.println("欢迎进入小说管理系统");

System.out.println("请选择菜单");

System.out.println("1.上传小说");

System.out.println("2.查看所有小说");

System.out.println("3.删除小说");

System.out.println("4.下载小说");

System.out.println("5.阅读小说");

//1.反序列化

JQNovelTool.deserialize();

String input = new Scanner(System.in).nextLine();

if (!input.matches("\\d")){

System.out.println("请输入对应选项");

continue;

}

int sel = Integer.parseInt(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;

}

}

}

}

工具类:JQNovelTool

public class JQNovelTool {

private static String savePath = "D:/novels";

private static String serializePath = "D:/novels/serialize";

private static List novelsList = new ArrayList();

static {

File file = new File(savePath);

if (!file.exists()){

file.mkdirs();

}

file = new File(serializePath);

if (!file.exists()){

try {

file.createNewFile();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

/**

* 上传小说*/

public static boolean upload() throws IOException{

String novelName;

String novelAuthor;

String uploadPath;

System.out.println("请输入上传小说名:");

novelName = new Scanner(System.in).nextLine();

System.out.println("请输入上传小说作者:");

novelAuthor = new Scanner(System.in).nextLine();

System.out.println("请输入上传小说路径:");

uploadPath = new Scanner(System.in).nextLine();

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.获取用户输入编号

int sel = new Scanner(System.in).nextInt();

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.获取用户输入编号

int sel = new Scanner(System.in).nextInt();

//3.判断是不中存在该文件

Novel novel = getNovel(sel);

if (novel == null)

return false;

//2.获取用户输入的目标路径

System.out.println("请输入目标路径");

String path = new Scanner(System.in).nextLine();

return downloadFile(novel,path);

}

/**

* 读取小说*/

public static boolean read() throws IOException{

//1.显示当前小说列表

getAllNovels();

//2.获取用户输入编号

String input = new Scanner(System.in).nextLine();

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(serializePath);

ObjectInputStream objInStream = new ObjectInputStream(inStream);

try{

@SuppressWarnings (value={"unchecked"})

List object = (ArrayList) objInStream.readObject();

novelsList = object;

}catch(Exception e){

e.printStackTrace();

}

objInStream.close();

}

/**

* 从指定路径上传文件到数据库*/

private static boolean uploadFile(Novel novel,String oriPath) throws IOException{

//1.判断目标路径是否存在

File oriFile = new File(oriPath);

if(!oriFile.exists()){

return false;

}

//2.创建管道流

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;

}

/**

* 删除文件*/

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 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){

System.out.println("1.首页 2.上一页 3.下一页 4.尾页 5.退出阅读");

String input = new Scanner(System.in).nextLine();

if (!input.matches("\\d")){

System.out.println("请输入对应选项");

continue;

}

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 currentLine = 0;

int count =0;

//通过便历文件内容的方式来获取字符个数,虽然感觉很傻B,但是目前也没有什么好办法

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 it = novelsList.iterator();

while(it.hasNext()){

System.out.println(it.next());

}

}

}

小说类:Novel

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() {

// TODO Auto-generated method stub

return this.getId()+"\t"+this.getName()+"\t"+this.getAuthor();

}

}

Paste_Image.png

Paste_Image.png

Paste_Image.png

java 小说系统_java 实现小说管理系统相关推荐

  1. java简单系统_Java简单学生管理系统

    Java简单学生管理系统 这个不需要手动输入,笔记记录 //student` public class student(){ private String id;//学号 private String ...

  2. java物业系统源码 物业管理系统源码

    java物业系统源码 物业管理系统源码 系统功能介绍 呼叫管理:系统自带呼叫模块,也可以对接专业呼叫系统,实现来电弹屏,业主自动识别,电话出入录音. 接待中心:解决诉求的接入过程,客户现场.电话.网上 ...

  3. java cs系统_Java课程设计——基于CS模式的用户管理系统

    Java笔记 Java项目设计思路: MVC设计模式: M:model:数据模型,类似于数据库中得一张表. V:view:视图,呈现给用户得操作界面,用户点击按钮等操作后会展示一些界面等. C:con ...

  4. 轻量级小说系统——狂雨小说cms v1.2.1

    狂雨小说内容管理系统(以下简称KYXSCMS)提供一个轻量级小说网站解决方案,基于ThinkPHP5.1+MySQL的技术开发.     KYXSCMS,灵活,方便,人性化设计简单易用是最大的特色,是 ...

  5. java物业管理系统设计_JAVA版物业管理系统论文+设计源码

    内容介绍 原文档由会员 从头再来 发布 (内含完整源代码) 1.2万字 28页 [摘要] 物业管理系统是紧随当今时代发展的需要,目的在于实现不同的人员对物业系统的不同的需要,有利于社会的稳定和顺利发展 ...

  6. java电话计费系统_java 连接数据库开发的电话计费管理系统

    [实例简介] 这是java连接数据库 sql 2000 开发的电话计费管理系统 里面运用的还是比较方便的虽然有一点小问题 [实例截图] [核心代码] 电话计费系统 └── 电话计费系统 ├── all ...

  7. java 打分系统_java学生日常评分管理系统

    每天记录学习,每天会有好心情.*^_^* 今天和一个朋友共同完成了一个基于java的学生日常评分管理系统项目,我们在开发时选用的框架是SSM(MYECLIPSE)框架.我这个朋友知识有限,只会这个框架 ...

  8. java 档案管理 系统_java教师档案管理系统

    每天记录学习,每天会有好心情.*^_^* 今天将为大家分析一个基于web的java教师档案管理系统,采用当前非常流行的B/S体系结构,以JAVA作为开发技术,主要依赖SSM技术框架,mysql数据库建 ...

  9. java 实验室预约系统_java实验室预约管理系统(源码+数据库脚本)

    随着高校教学资源逐步开发,高校教育资源的应用种类逐步增多.而实验室作为高校基本教育资源之一,与日常教学工作开展.课堂项目实践有着密切联系.由此,如何借助新时期技术,加强高校实验室应用数据信息程序开发, ...

  10. java物流系统_java 物流管理系统

    [实例简介] java swing实现的物流管理系统,内含SQL 2005的数据库脚本文件.实现了产品管理,制造商管理,物流管理,仓库管理,零售商管理等等.适合初学者,界面简洁美观 [实例截图] [核 ...

最新文章

  1. 武汉一高中14名学生被剑桥牛津预录取!逻辑思路比结果更重要
  2. 回文串 --- 动态dp UVA 11584
  3. 网站微信登录授权 ASP.NET
  4. 泛微e9隐藏明细表_泛微E8 隐藏行、明细表
  5. DCMTK:dicom标签的基础类
  6. java 写文件 0x0d_Java 读写文件 - My and My Princess…… - OSCHINA - 中文开源技术交流社区...
  7. java凯撒密码_JAVA凯撒密码 选择问题
  8. 【POJ - 2398】Toy Storage (计算几何,二分找位置,叉积,点和直线的位置关系)
  9. 蓝桥杯第六届省赛JAVA真题----打印菱形
  10. 在Python中写入文件时,权限被拒绝错误
  11. 【Spring】Spring注解配置okhttp3
  12. png免扣半透素材,让你轻松设计出漂亮的海报!
  13. 监管大屏系统_“警视”警务情指一体大屏可视化决策系统
  14. 为什么每个邮件收到后都会有一个htm的附件_职场邮件:领导、同事都喜欢收到的邮件丨邮件技巧...
  15. XML 转 JSON
  16. 求单链表的交集和并集
  17. 树莓派raspberryPI-4b 官方镜像raspios-bullseye-arm64 系統下编译构建ros2 rolling环境(附下载完整镜像资料)
  18. 今天看了ning的介绍,很有意思
  19. 《网络是怎样连接的》学习(一、浏览器)
  20. 如何轻松把mysql数据表对齐?!正解在这儿

热门文章

  1. 自建nod32更新服务器,ESET NOD32 官方升级服务器地址
  2. 第17章 其他数据库日志【4.日志与备份篇】【MySQL高级】
  3. 看黄天鹅如何下一颗高端鸡蛋?
  4. 系统重装后恢复MySQL数据
  5. MSTAR雷达数据集总结
  6. 码云 zheng 项目部署过程记录 eclispe部署过程
  7. Intel NUC10i7FNH 寒霜峡谷测试体验
  8. SUN SPARC T4-4电源故障引起的宕机
  9. [机缘参悟-1] - 活在当下,仰望星空,梦在梦里,俯视天下
  10. 冒险岛左右移动攻击,定时加血加蓝加BUFF JAVA实现