Java简易小说阅读器
小說(shuō)閱讀器`
剛學(xué)了java
第一次做的小系統(tǒng) !
有瑕疵見(jiàn)諒
提前準(zhǔn)備好一個(gè)文件夾Reabooooook
Readbooooook下Bookcase (書架) Bookmall(書城) register(注冊(cè)) 文件
Bookcase下 各個(gè)分類文件 分類文件里menu.txt 以及兩本書的內(nèi)容
Bookmall同上
register下register.txt
package System;import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Readbook {
/**
- 判斷是否為vip 下載是否收費(fèi)
*/
Boolean bool ;
/**
- 書籍名字
*/
String bookname;
/**
- 存每個(gè)書籍的讀取地址
*/
File fi ;
/**
- 書架里初始有兩本書
*/
int i =2;
/**
- 注冊(cè)的方法
*/
public void Regist(){
Scanner sc = new Scanner(System.in);
Register reg = new Register();
FileWriter fw = null;
/**
- 注冊(cè)并存入register.txt文件里
*/
try {
File file = new File(“D:\Readbooooook\register\register.txt”);
// file.mkdirs();
fw = new FileWriter(file,true);
while(true){
System.out.println(“請(qǐng)輸入用戶名:”);
String name = sc.next();
reg.setName(name);
fw.write(name);fw.write(",");
System.out.println(“請(qǐng)輸入密碼:”);
String pwd1 = sc.next();
fw.write(pwd1);fw.write(";");
System.out.println(“請(qǐng)?jiān)俅屋斎朊艽a:”);
String pwd2 = sc.next();
if(pwd1.equals(pwd2)){
System.out.println(“注冊(cè)成功!\n”);
reg.setPwd1(pwd1);
reg.setPwd2(pwd2);
break;
}else {System.out.println(“注冊(cè)失敗,請(qǐng)重新輸入”);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
- 登錄方法
*/
public void log(){
Scanner sc = new Scanner(System.in);
int i = 0;
while(i<5){
System.out.println(“請(qǐng)輸入用戶名”);
String name = sc.next();
System.out.println(“請(qǐng)輸入密碼”);
String password = sc.next();
boolean bo = login(name,password);
if(botrue){
System.out.println("登陸成功 ");break;
}else{
System.out.println(“用戶名/密碼錯(cuò)誤,請(qǐng)重新輸入,還有”+(4-i)+“次機(jī)會(huì)”);
i++;
}
}if(i5){
System.out.println(“很抱歉用戶!您的錯(cuò)誤次數(shù)已用完!”);
}
}
/**
- 判斷是否登錄成功的方法
- 與存在register里的數(shù)據(jù)進(jìn)行比較
- @param name
- @param password
- @return
*/
public boolean login(String name,String password){
boolean isSuccess = false;
try {
fis = new FileReader(“D:\Readbooooook\register\register.txt”);
char[] c = new char[1024];
StringBuffer sbffer = new StringBuffer();
int length = 0;
while((length = fis.read?) != -1){
sbffer.append(c,0,length);
}
String str = sbffer.toString(); //賬號(hào),密碼
String[] userArr = str.split(";");
for(int i = 0 ; i < userArr.length ; i++){
//在此處可以驗(yàn)證登錄成功與否
String[] userMsg = userArr[i].split(",");
if(userMsg[0].equals(name)&&userMsg[1].equals(password)){
isSuccess = true;break;
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return isSuccess;
}
/**
- 書城的方法
- 選擇后直接調(diào)各自類型的方法
*/
public void bookmall(){
Scanner sc = new Scanner(System.in);
System.out.println(“請(qǐng)選擇小說(shuō)類型:\n1.古代\t2.競(jìng)技\t3.玄幻\t4.免費(fèi)”);
int a = sc.nextInt();
switch(a){
case 1:gudai();break;
case 2:jingji();break;
case 3:xuanhuan();break;
case 4:free();break;
} }
/**
- 古代書類的方法
/
public void gudai(){
Scanner sc = new Scanner(System.in);
FileReader fr = null;
try{
/*- 讀取古代書類的menu
*/
fr = new FileReader(“D:\Readbooooook\Bookmall\古代\menu.txt”);
char[] c = new char[1024];
int length = 0;
while((length = fr.read?)!=-1){
String str = new String(c,0,length);
System.out.println(str);
}
//選擇書
System.out.println(“請(qǐng)選擇感興趣的小說(shuō):”);
int a = sc.nextInt();
switch(a){
case 1:System.out.println(“您已選擇【雪中悍刀行】”);
bookname = “雪中悍刀行”;
fi = new File(“D:\Readbooooook\Bookmall\古代\雪中悍刀行.txt”);
break;
case 2:System.out.println(“您已選擇【長(zhǎng)安十二時(shí)辰】”);
bookname = “長(zhǎng)安十二時(shí)辰”;
fi = new File(“D:\Readbooooook\Bookmall\古代\長(zhǎng)安十二時(shí)辰.txt”);
break;
default:System.out.println(“您選擇的書籍不存在!”);
}
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- 讀取古代書類的menu
/**
- 競(jìng)技書類的方法
*/
public void jingji(){
Scanner sc = new Scanner(System.in); FileReader fr = null;
try{
fr = new FileReader(“D:\Readbooooook\Bookmall\競(jìng)技\menu.txt”);
char[] c = new char[1024];
int length = 0;
while((length = fr.read?)!=-1){
String str = new String(c,0,length);
System.out.println(str);
}
System.out.println(“請(qǐng)選擇感興趣的小說(shuō):”);
int a = sc.nextInt();
switch(a){
case 1:System.out.println(“您已選擇【全職高手】”);
bookname = “全職高手”;
fi = new File(“D:\Readbooooook\Bookmall\競(jìng)技\全職高手.txt”);
break;
case 2:System.out.println(“您已選擇【英雄聯(lián)盟之誰(shuí)與爭(zhēng)鋒】”);
bookname = “英雄聯(lián)盟之誰(shuí)與爭(zhēng)鋒”;
fi = new File(“D:\Readbooooook\Bookmall\競(jìng)技\英雄聯(lián)盟之誰(shuí)與爭(zhēng)鋒.txt”);
break;
default:System.out.println(“您選擇的書籍不存在!”);
}
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
- 玄幻書類的方法
*/
public void xuanhuan(){
Scanner sc = new Scanner(System.in); FileReader fr = null;
try{
fr = new FileReader(“D:\Readbooooook\Bookmall\玄幻\menu.txt”);
char[] c = new char[1024];
int length = 0;
while((length = fr.read?)!=-1){
String str = new String(c,0,length);
System.out.println(str);
}
System.out.println(“請(qǐng)選擇感興趣的小說(shuō):”);
int a = sc.nextInt();
switch(a){
case 1:System.out.println(“您已選擇【吞噬星空】”);
bookname = “吞噬星空”;
fi = new File(“D:\Readbooooook\Bookmall\玄幻\吞噬星空.txt”);
break;
case 2:System.out.println(“您已選擇【星辰變】”);
bookname = “星辰變”;
fi = new File(“D:\Readbooooook\Bookmall\玄幻\星辰變.txt”);
break;
default:System.out.println(“您選擇的書籍不存在!”);
}
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
- 免費(fèi)書類的方法
*/
public void free(){
Scanner sc = new Scanner(System.in);
FileReader fr = null;
bool = true;//無(wú)論是否vip都免費(fèi) try{
fr = new FileReader(“D:\Readbooooook\Bookmall\免費(fèi)\menu.txt”);
char[] c = new char[1024];
int length = 0;
while((length = fr.read?)!=-1){
String str = new String(c,0,length);
System.out.println(str);
}
System.out.println(“請(qǐng)選擇感興趣的小說(shuō):”);
int a = sc.nextInt();
switch(a){
case 1:System.out.println(“您已選擇【斗羅大陸】”);
bookname = “斗羅大陸”;
fi = new File(“D:\Readbooooook\Bookmall\免費(fèi)\斗羅大陸.txt”);
break;
case 2:System.out.println(“您已選擇【斗破蒼穹】”);
bookname = “斗破蒼穹”;
fi = new File(“D:\Readbooooook\Bookmall\免費(fèi)\斗破蒼穹.txt”);
break;
default:System.out.println(“您選擇的書籍不存在!”);
}
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void print(){
/**
- 選擇功能的方法
/
Scanner sc = new Scanner(System.in);
System.out.println(“1.添加至?xí)躙n2.立即閱讀\n3.立即下載”);
int a = sc.nextInt();
switch(a){
case 1:System.out.println(“添加成功!跳轉(zhuǎn)至?xí)堋?;
add();bcase();
break;
case 2:read();
System.out.println("********************");
break;
case 3:load();break;
}
}
public void add( ){
/**
- 添加到書架的方法
*/
FileReader fr = null; //讀小說(shuō)
FileWriter fw = null;
FileWriter fww = null; //復(fù)制小說(shuō)
File fff = null;
i++; //每添加一次書架 i+1
try {
fr = new FileReader(fi); //讀到源文件里的小說(shuō) // 書名寫進(jìn)書架\目錄.txt里
fff = new File(“D:\Readbooooook\Bookcase\menu.txt”);
fw = new FileWriter(fff,true);
fw.write(i+".");
fw.write(bookname);
//小說(shuō)內(nèi)容復(fù)制到書架\new.txt里
char[] c = new char[1024];
int len = 0;
while((len=fr.read?)!=-1){
String str = new String(c,0,len);
//根據(jù)書架的第幾本書自動(dòng)建新new i 文件
fww = new FileWriter(“D:\Readbooooook\Bookcase\new”+i+".txt");
fww.write(str);
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fww.close();
fw.close();
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
-
立即閱讀的方法
*/
public void read(){
FileReader fr = null;
try {
fr = new FileReader(fi);
int length = 0;
char[] c = new char[1024];while((length = fr.read?)!=-1){
String str = new String(c,0,length);
System.out.println(str);
}System.out.println("<<<小說(shuō)閱讀完成>>>"); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
- 下載小說(shuō)的方法
*/
public void load(){ Scanner sc = new Scanner(System.in);
if(boolfalse){
//false則需要花錢
System.out.println("<本次下載自動(dòng)免密支付1書幣!>");
System.out.println("<<自動(dòng)添加至?xí)?gt;>");
}else if(booltrue){
//true就免費(fèi)
System.out.println("<免費(fèi)下載并添加到書架成功!>");
}
//下載完成自動(dòng)添加至?xí)?br /> add();
System.out.println("[是否立即閱讀?(y/n)否則跳轉(zhuǎn)至?xí)躚");
String a = sc.next();
if(a.equals(“y”)){
read();
}else{
bcase();
}
}
/**
- 書架的方法
*/
public void bcase(){ Scanner sc = new Scanner(System.in);
FileReader fr = null;
FileReader fr1 = null; char[] c = new char[1024];
int length = 0;
try {
//顯示書架目錄
fr = new FileReader(“D:\Readbooooook\Bookcase\menu.txt”);
while((length = fr.read?)!=-1){
String string = new String(c,0,length);
System.out.println(string);
}
System.out.println("[閱讀1 or 退出2]");
int i = sc.nextInt();
if(i==1){
System.out.println(“請(qǐng)選擇書籍[編號(hào)]”);
int a = sc.nextInt();
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>\n");
//書架里第一本書的讀取
fr1 = new FileReader(“D:\Readbooooook\Bookcase\new”+a+".txt");
while((length = fr1.read?)!=-1){
String s = new String(c,0,length);
System.out.println(s);
}System.out.println("-----小說(shuō)閱讀完成-----");
}else if(i==2){
System.out.println("<退出書架!>");
}else {
System.out.println("<<輸入錯(cuò)誤!>>");
//錯(cuò)誤則重新走書架方法
bcase();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fr.close();
if(fr1 != null){
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
- vip方法
*/
public void VIP(){
Scanner sc = new Scanner(System.in);
System.out.println(“親是否充值vip(?ˉ?ˉ?)?可享受全場(chǎng)小說(shuō)下載免費(fèi)!”);
String a = sc.next();
if(a.equals(“是”)){
//充值賦值bool為true
bool = true;
System.out.println(“是否立即支付y/n”);
String b = sc.next();
if(b.equals(“y”)){
System.out.println(“免密支付成功!”);
}else {
System.out.println(“支付失敗”);
//支付不成功則再次賦值false
bool = false;
}
}else{
bool = false;
}
}
}
`
主方法
【不知道為什么一復(fù)制過(guò)來(lái)就亂了
總結(jié)
以上是生活随笔為你收集整理的Java简易小说阅读器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java学习(55):定义一个抽象类的继
- 下一篇: 会返回两次_嫦娥五号为何用独特的半弹道式