需求:实现下面功能

欢迎进入小说管理系统

请选择菜单

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 实现小说管理系统