java 小说系统_java 实现小说管理系统
需求:實(shí)現(xiàn)下面功能
歡迎進(jìn)入小說(shuō)管理系統(tǒng)
請(qǐng)選擇菜單
1.上傳小說(shuō)
2.查看所有小說(shuō)
3.刪除小說(shuō)
4.下載小說(shuō)
5.閱讀小說(shuō)
閱讀小說(shuō)時(shí)要實(shí)現(xiàn)分頁(yè)功能,每頁(yè)顯示100個(gè)字符且有以下選項(xiàng)
1.首頁(yè) 2.上一頁(yè) 3.下一頁(yè) 4.尾頁(yè) 5.退出閱讀
提示:
創(chuàng)建一個(gè)小說(shuō)類(小說(shuō)編號(hào),小說(shuō)名,作者,上傳后的路徑);用一個(gè)list來(lái)存儲(chǔ)當(dāng)前的小說(shuō)數(shù)量;固定一個(gè)用戶目錄用來(lái)存儲(chǔ)用戶上傳的小說(shuō);閱讀功能可以用一個(gè)變量來(lái)標(biāo)記當(dāng)前頁(yè);
思路:
上傳小說(shuō),就把用戶指定的路徑下的文件通過(guò)流復(fù)制到我們事先設(shè)定好的一個(gè)文件夾中
查看所有小說(shuō)時(shí),直接遍歷list即可
通過(guò)遍歷查找對(duì)應(yīng)id的小說(shuō)對(duì)象,然后從list中刪除該對(duì)象,最后把該對(duì)象中存儲(chǔ)的小說(shuō)路徑下的小說(shuō)刪除
下載小說(shuō)可以從小說(shuō)對(duì)象的路徑中復(fù)制到用戶指定目錄
閱讀分頁(yè)需要知道小說(shuō)字符個(gè)數(shù),然后/100來(lái)編號(hào),最后使用skip()函數(shù)來(lái)達(dá)到目的
給list存檔,每次選擇系統(tǒng)功能前都先要反序列化出來(lái)list;每次執(zhí)行完上傳,刪除功能后都應(yīng)該序列化list;
入口函數(shù):Main函數(shù)
public class Main {
public static void main(String[] args) throws IOException, ClassNotFoundException {
while(true){
System.out.println("歡迎進(jìn)入小說(shuō)管理系統(tǒng)");
System.out.println("請(qǐng)選擇菜單");
System.out.println("1.上傳小說(shuō)");
System.out.println("2.查看所有小說(shuō)");
System.out.println("3.刪除小說(shuō)");
System.out.println("4.下載小說(shuō)");
System.out.println("5.閱讀小說(shuō)");
//1.反序列化
JQNovelTool.deserialize();
String input = new Scanner(System.in).nextLine();
if (!input.matches("\\d")){
System.out.println("請(qǐng)輸入對(duì)應(yīng)選項(xiàng)");
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("已上傳的小說(shuō)");
JQNovelTool.showNovels();
}
break;
case 3:{
System.out.println("請(qǐng)輸入您要?jiǎng)h除的小說(shuō)編號(hào)");
if (JQNovelTool.remove()){
System.out.println("刪除成功");
//序列化
JQNovelTool.serialize();
}else{
System.out.println("沒(méi)有對(duì)應(yīng)小說(shuō)編號(hào)");
}
}
break;
case 4:{
System.out.println("請(qǐng)輸入您要下載的小說(shuō)編號(hào)");
if(JQNovelTool.download())
System.out.println("下載成功");
else
System.out.println("沒(méi)有對(duì)應(yīng)小說(shuō)編號(hào)或目錄");
}
break;
case 5:{
System.out.println("請(qǐng)輸入您要閱讀的小說(shuō)編號(hào)");
if (!JQNovelTool.read())
System.out.println("沒(méi)有對(duì)應(yīng)小說(shuō)編號(hào)");
}
break;
default:
System.out.println("暫時(shí)沒(méi)有對(duì)應(yīng)的功能,敬請(qǐng)期待");
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();
}
}
}
/**
* 上傳小說(shuō)*/
public static boolean upload() throws IOException{
String novelName;
String novelAuthor;
String uploadPath;
System.out.println("請(qǐng)輸入上傳小說(shuō)名:");
novelName = new Scanner(System.in).nextLine();
System.out.println("請(qǐng)輸入上傳小說(shuō)作者:");
novelAuthor = new Scanner(System.in).nextLine();
System.out.println("請(qǐng)輸入上傳小說(shuō)路徑:");
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;
}
/**
* 顯示所有小說(shuō)信息*/
public static void showNovels(){
getAllNovels();
}
/**
* 刪除小說(shuō)*/
public static boolean remove(){
//1.顯示當(dāng)前小說(shuō)列表
getAllNovels();
//2.獲取用戶輸入編號(hào)
int sel = new Scanner(System.in).nextInt();
if (sel<0 || sel>=novelsList.size())
return false;
//3.查找對(duì)應(yīng)編號(hào)小說(shuō)
Novel novel = getNovel(sel);
//4.刪除小說(shuō)
new File(novel.getUploadPath()).delete();
return novelsList.remove(novel);
}
/**
* 下載小說(shuō)到用戶指定路徑*/
public static boolean download() throws IOException{
//1.顯示當(dāng)前小說(shuō)列表
getAllNovels();
//2.獲取用戶輸入編號(hào)
int sel = new Scanner(System.in).nextInt();
//3.判斷是不中存在該文件
Novel novel = getNovel(sel);
if (novel == null)
return false;
//2.獲取用戶輸入的目標(biāo)路徑
System.out.println("請(qǐng)輸入目標(biāo)路徑");
String path = new Scanner(System.in).nextLine();
return downloadFile(novel,path);
}
/**
* 讀取小說(shuō)*/
public static boolean read() throws IOException{
//1.顯示當(dāng)前小說(shuō)列表
getAllNovels();
//2.獲取用戶輸入編號(hào)
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();
}
/**
* 從指定路徑上傳文件到數(shù)據(jù)庫(kù)*/
private static boolean uploadFile(Novel novel,String oriPath) throws IOException{
//1.判斷目標(biāo)路徑是否存在
File oriFile = new File(oriPath);
if(!oriFile.exists()){
return false;
}
//2.創(chuàng)建管道流
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.創(chuàng)建文件
String line = "";
while((line = reader.readLine())!=null){
writer.write(line);
}
//4.給novel設(shè)置最終存儲(chǔ)路徑
novel.setUploadPath(tarFile.getAbsolutePath());
//5.關(guān)閉管道
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;
}
/**
* 下載小說(shuō)文件*/
private static boolean downloadFile(Novel novel,String desPath) throws IOException{
//1.判斷目標(biāo)文件是否存在且不是文件
File desfile = new File(desPath);
if (!desfile.exists() || desfile.isFile()){
return false;
}
//2.得到數(shù)據(jù)庫(kù)中小說(shuō)且創(chuàng)建管道
File oriFile = new File(novel.getUploadPath());
BufferedReader reader = new BufferedReader(new FileReader(oriFile));
//3.設(shè)置目標(biāo)位置且創(chuàng)建管道
BufferedWriter writer = new BufferedWriter(new FileWriter(desfile+File.separator+novel.getName()+".txt"));
String line = "";
while((line = reader.readLine())!=null){
writer.write(line);
}
//4.關(guān)閉通道
reader.close();
writer.close();
return true;
}
/**
* 通過(guò)索取獲取list中的novel對(duì)象,沒(méi)有則返回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;
}
/**
* 分頁(yè)讀取小說(shuō)內(nèi)容*/
private static void read(Novel novel) throws IOException{
int curRow = 0;//當(dāng)前閱讀所在的頁(yè)數(shù)
int count = getCharCount(novel);//獲取所有字符個(gè)數(shù)
int rowCount = 0;//可以分為多少頁(yè)
if (count%100 == 0){
rowCount = count/100;
}else{
rowCount = count/100+1;
}
//傳入指定頁(yè)碼,獲取對(duì)應(yīng)的100個(gè)字符
System.out.println(getReadStr(novel,curRow));
//閱讀選項(xiàng)
while (true){
System.out.println("1.首頁(yè) 2.上一頁(yè) 3.下一頁(yè) 4.尾頁(yè) 5.退出閱讀");
String input = new Scanner(System.in).nextLine();
if (!input.matches("\\d")){
System.out.println("請(qǐng)輸入對(duì)應(yīng)選項(xiàng)");
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("已到首頁(yè)");
}
break;
case 3:
curRow += 1;
if (curRow>=rowCount){ //不能讓其越界
curRow = rowCount-1;
System.out.println("已到尾頁(yè)");
}
break;
case 4:
curRow = rowCount-1;
break;
case 5:
return;
default:
System.out.println("沒(méi)有該項(xiàng)操作");
}
System.out.println(getReadStr(novel,curRow));
}
}
/**
* 根據(jù)小說(shuō)的路徑,獲取文件的字符個(gè)數(shù)(字符可以是英文,也可以是中文)*/
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;
//通過(guò)便歷文件內(nèi)容的方式來(lái)獲取字符個(gè)數(shù),雖然感覺(jué)很傻B,但是目前也沒(méi)有什么好辦法
while(fileReader.read()!=-1){
count++;
}
fileReader.close();
return count;
}
/**
* 讀取給定小說(shuō)的頁(yè)碼內(nèi)容*/
private static String getReadStr(Novel novel,int curRow) throws IOException{
//1.取出小說(shuō)對(duì)應(yīng)的絕對(duì)路徑
File oriFile = new File(novel.getUploadPath());
FileReader fileReader = new FileReader(oriFile);
fileReader.skip(curRow * 100); //關(guān)鍵部分,使用skip函數(shù)
//2.每次讀取100個(gè)字符
char buf[] = new char[100];
fileReader.read(buf);
fileReader.close();
//3.返回buf
return new String(buf);
}
/*如果最后的內(nèi)容不夠100個(gè)字符也不會(huì)有問(wèn)題,該read(buf)只是盡力的去填滿這100的容量,不夠就把剩余的內(nèi)容裝進(jìn)去就好*/
/**
* 獲取list中所有小說(shuō)*/
private static void getAllNovels(){
System.out.println("小說(shuō)編號(hào)\t小說(shuō)名稱\t小說(shuō)作者");
Iterator it = novelsList.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
小說(shuō)類: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
總結(jié)
以上是生活随笔為你收集整理的java 小说系统_java 实现小说管理系统的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 企业支付宝转账到银行卡(免费率 无限额)
- 下一篇: spring核心技术之Resource资