之前看到一个在MyEclipse console 看糗百的,看了下,不是很复杂。上班时候会偷偷懒,看会小说,有天就想写一个在控制台看小说的程序,前不久经过几次优化已经写完了。
文件路径可以自行修改,按回车自动翻页,每次翻页前自动保存进度,进度保存在文件目录的*.properties文件中。
下面是代码段:
package file;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Properties;
import java.util.Scanner;
public class NovelReader {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String fileName ="E:\\《悟空传》精校版.txt";
String proNameString = fileName.replace("txt", "properties");
File file = new File(fileName);
//RandomAccessFile file2 = new RandomAccessFile(file, "rw");
InputStream inputStream = new FileInputStream(file);
byte[] head = new byte[3];
inputStream.read(head);
//获取文件的编码格式
String code = "";
code = "GB2312";
if (head[0] == -1 && head[1] == -2 )
code = "UTF-16";
if (head[0] == -2 && head[1] == -1 )
code = "Unicode";
if(head[0]==-17 && head[1]==-69 && head[2] ==-65)
code = "UTF-8";
//第一次扫描文件,统计文件的总行数
InputStreamReader isr = new InputStreamReader(new FileInputStream(file),code);
BufferedReader br = new BufferedReader(isr);
String tmp=null;
int count=0;
while ((tmp=br.readLine())!=null) {
count++;
}
isr.close();
//System.out.println("总行数为"+count);
//设置一页读取的行数
int pagesize=5;
int pageno =0;
//获取总页数
pageno=count%pagesize==0?count/pagesize:count/pagesize+1;
//从文件中读取保存的进度
int pageindex=ReadProperties(proNameString,"pageindex");
System.out.println("您上次读到"+pageindex+"页");
System.out.println("请输入回车读取下一页,或输入1开头退出:");
Scanner s = new Scanner(System.in);
s.useDelimiter("\n");
InputStreamReader isr2 = new InputStreamReader(new FileInputStream(file), code);
BufferedReader br2 = new BufferedReader(isr2);
//跳过已读取的行数
for (int i = 0; i < pageindex*pagesize; i++) {
br2.readLine();
}
//开始读取
int readedCount =0;
int linesize=60;
String tmp2=null;
while((tmp2=s.nextLine())!=null&&pageindex<pageno){
if(tmp2.equals("1")){
writeProperties(proNameString, "pageindex",String.valueOf(pageindex));
System.out.println(proNameString);
break;
}
pageindex++;
System.out.println("--------------------------------------------第"+pageindex+"页----------------------------------------");
for (int i = 0; i < pagesize; i++) {
String temp =null;
if((temp=br2.readLine())!=null){
readedCount+=temp.length();
while(temp.length()>linesize){
System.out.println(temp.substring(0, linesize+1));
temp=temp.substring(linesize+1);
}
System.out.println(temp);
}
}
//每次读取完一页,保存页数
writeProperties(proNameString, "pageindex",String.valueOf(pageindex-1));
}
isr2.close();
}
//保存页数到文件
public static void writeProperties(String proFile,String name,String value){
Properties p = new Properties();
try {
File profile = new File(proFile);
if(!profile.exists()){
profile.createNewFile();
}
InputStream in = new FileInputStream(proFile);
p.load(in);//从属性中读取属性列表
OutputStream out = new FileOutputStream(proFile);
p.setProperty(name, value);
p.store(out, "Update"+name+"value");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//读取文件的页数
public static int ReadProperties(String proFile,String name) {
Properties p = new Properties();
try {
File profile = new File(proFile);
if(profile.exists()){
InputStream in = new FileInputStream(proFile);
p.load(in);
String pageindex =p.getProperty(name, "0");
return Integer.parseInt(pageindex);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
}
欢迎指正!