java實現小說閱讀器(功能:檢視全文、統計總行數/總頁碼數、查詢指定行、指定頁碼、實現翻頁)
阿新 • • 發佈:2019-01-14
1.任務要求:
在DOS視窗下,實現檢視全文、統計總行數/總頁碼數、查詢指定行、指定頁碼、翻頁
2.閱讀器實現方式
輸入1(檢視全文、統計總行數/總頁碼數、)
輸入2(讀取指定行,查詢其上下行)
輸入+顯示下一行,輸入-顯示上一行,輸入bye返回上一級
輸入3(查詢指定頁碼,實現翻頁功能)
輸入+顯示下一頁,輸入-顯示上一頁,輸入bye返回上一級
3.具體程式碼如下
主要分為三個子程式,然後在主程式中分別進行呼叫,以此來實現小說閱讀器的各功能。
import java.io.*;
import java.util.Scanner;
class Display_txt
{
//檢視全文(以每10個段落為一頁,統計總行數)
static void ReadAll (String fileName) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader (new FileInputStream (fileName)));//使用緩衝區的方法將資料讀入到緩衝區中
String str;
int i=1;
int a=0,b=1;
while((str = br.readLine()) != null)
{
System.out.println( "["+i+"]:" + str);
System.out.println( );
if(i==a+10) //分頁(以每10個段落為一頁)
{
System.out.println("------------------------ page "+b+" ------------------------");
a=a+10;
b=b+1;
}
i=i+1;
}
System.out.println("------------------------ page "+b+" ------------------------\n" );
i=i-1;
System.out.println("本文共有"+i +"行; 共有"+b+"頁\n");
}
//檢視指定頁碼,實現上下翻頁
static void getPage (String fileName) throws IOException
{
System.out.println("(提示:輸入00返回上一級)");
System.out.print("請輸入查詢頁碼:");
Scanner sc=new Scanner(System.in);
int pageNumber=sc.nextInt();
while(pageNumber!=00)
{
BufferedReader br = new BufferedReader(new InputStreamReader (new FileInputStream (fileName)));
String line = br.readLine();
int num=0;
int startNumber=(pageNumber-1)*10+1;
int endNumber=pageNumber*10;
while(line!= null)
{
if(startNumber==++num)
{
System.out.println("["+startNumber+"]:"+line);
int i=startNumber;
while(i<endNumber)
{
i=i+1;
line=br.readLine();
System.out.println("["+i+"]:"+line);
}
System.out.println("-------------------page "+ pageNumber+"--------------------");
System.out.println("(提示:輸入+(下一頁);輸入-(上一頁);輸入bye(返回上一級);");
System.out.print("請輸入:");
Scanner sc1=new Scanner(System.in);
String pageNumber1=sc1.next();
int pn=pageNumber;
while(!pageNumber1.equals("bye"))
{
if(pageNumber1.equals("+"))
{
BufferedReader br1 = new BufferedReader(new InputStreamReader (new FileInputStream (fileName)));
String line1=br1.readLine();
pn=pn+1;
int num1=0;
int startNumber1=(pn-1)*10+1,endNumber1=pn*10;
while(line1!= null)
{
if (startNumber1==++num1)
{
System.out.println("["+startNumber1+"]:"+line1);
int a=startNumber1;
while(a<endNumber1)
{
a=a+1;
line1=br1.readLine();
System.out.println("["+a+"]:"+line1);
}
System.out.println("-------------------page "+ pn+"--------------------");
}
line1=br1.readLine();
}
}
if(pageNumber1.equals("-"))
{
BufferedReader br2 = new BufferedReader(new InputStreamReader (new FileInputStream (fileName)));
String line2=br2.readLine();
pn=pn-1;
int num2=0;
int startNumber2=(pn-1)*10+1,endNumber2=pn*10;
while(line2!= null)
{
if (startNumber2==++num2)
{
System.out.println("["+startNumber2+"]:"+line2);
int b=startNumber2;
while(b<endNumber2)
{
b=b+1;
line2=br2.readLine();
System.out.println("["+b+"]:"+line2);
}
System.out.println("-------------------page "+ pn+"--------------------");
}
line2=br2.readLine();
}
}
else
{}
System.out.print("請輸入:");
pageNumber1=sc1.next();
}
}
line=br.readLine();
}
System.out.println("(提示:輸入00返回上一級)");
System.out.print("請輸入查詢頁碼:");
pageNumber=sc.nextInt();
}
System.out.println("返回到上一級");
}
//讀取指定行,並檢視上下行
static void readxxLine (String fileName) throws IOException
{
System.out.println("(提示:輸入00返回上一級)");
System.out.print("請輸入查詢行:");
Scanner sc=new Scanner(System.in);
int lineNumber=sc.nextInt();
while(lineNumber!=00)
{
BufferedReader br = new BufferedReader(new InputStreamReader (new FileInputStream (fileName)));//使用緩衝區的方法將資料讀入到緩衝區中
String line = br.readLine();
int num=0;
while(line!= null)
{
if(lineNumber==++num)
{
System.out.println("["+lineNumber+"]:"+line);
System.out.println("提示:輸入+(下一行);輸入-(上一行);輸入bye(返回上一級)");
System.out.print("請輸入:");
Scanner sc1=new Scanner(System.in);
String lineNumber1=sc1.next();
int ln=lineNumber;
while(!lineNumber1.equals("bye"))
{
if(lineNumber1.equals("+"))
{
BufferedReader br1 = new BufferedReader(new InputStreamReader (new FileInputStream (fileName)));
String line1=br1.readLine();
ln=ln+1;
int num1=0;
while(line1!= null)
{
if (ln==++num1)
{
System.out.println("["+ln+"]:"+line1);
}
line1=br1.readLine();
}
}
if(lineNumber1.equals("-"))
{
BufferedReader br2 = new BufferedReader(new InputStreamReader (new FileInputStream (fileName)));
String line2=br2.readLine();
ln=ln-1;
int num2=0;
while(line2!= null)
{
if (ln==++num2)
{
System.out.println("["+ln+"]:"+line2);
}
line2=br2.readLine();
}
}
else
{}
lineNumber1=sc1.next();
}
}
line=br.readLine();
}
System.out.println("(提示:輸入00返回上一級)");
System.out.print("請輸入查詢行:");
lineNumber=sc.nextInt();
}
System.out.println("返回上一級");
}
//主程式
public static void main(String[] args) throws IOException
{
String fileName ="E:/java task/cookie reader/哲學家都幹了些什麼.txt";
System.out.println("提示:輸入1(檢視全文,統計全文總行數和總頁碼數);輸入2(讀取指定行,並檢視上下行);輸入3(檢視指定頁碼的內容,上下翻頁);");
while(true)
{
System.out.print("請輸入:");
Scanner sc=new Scanner(System.in);
int j=sc.nextInt();
if(j==1)
{
ReadAll(fileName);
j=0;
}
if(j==2)
{
readxxLine(fileName);
j=0;
}
if(j==3)
{
getPage(fileName);
j=0;
}
if(j==0)
{}
else
{
System.out.println("輸入錯誤");
}
System.out.println("提示:輸入1(檢視全文,統計全文總行數和總頁碼數);輸入2(讀取指定行,並檢視上下行);輸入3(檢視指定頁碼的內容,上下翻頁);");
}
}
}