java 小说系统_java 实现小说管理系统
需求:實現下面功能
歡迎進入小說管理系統
請選擇菜單
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 实现小说管理系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 企业支付宝转账到银行卡(免费率 无限额)
- 下一篇: i.max6 e9 android系统添