myeclipse console看小说

之前看到一个在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;
	}

}


欢迎指正!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值