java 控制台实现文本查看,行数可调,回车翻页

java 控制台实现文本查看,行数可调,回车翻页

实现如下:

public static void main(String[] args){
        FileInputStream fileInputStream = null;
        InputStreamReader r = null;
        BufferedReader reader = null;
        try {
            Scanner input = new Scanner(System.in);
            File file = new File("你的文件地址");
            //防止乱码,使用inputStreamReader 设置编码格式 gbk 和 fileInputStream
            fileInputStream = new FileInputStream(file);
            r = new InputStreamReader(fileInputStream, "你需要的编码格式");
            reader = new BufferedReader(r);

            String in;
            do {
                //清屏功能 可根据控制台显示范围调整 =_=!
                System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
                //每一页读取3行数据  行数可调整
                String writer;
                for (int i = 0; i < 3; i++) {
                    if ((writer = reader.readLine()) != null) {
                        if ("".equals(writer.trim())) {
                            i--;//补全打印行数
                            continue;//跳过空行
                        }
                        System.out.println(writer);
                    }
                }
                //判断是否下一页
                System.out.println("====>>");
                in = input.nextLine();
            } while ("".equals(in));
            System.out.println("退出程序!");
        }catch (IOException io){
            io.printStackTrace();
        }finally {
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                if (r != null) {
                    r.close();
                }
                if (reader != null) {
                    reader.close();
                }
            }catch (Exception e){
                System.out.println("关闭资源时报错!");
                e.printStackTrace();
            }
        }
    }

新版本-增加书签、一行截取功能:

public static void main(String[] args) {
        FileInputStream fs = null;
        InputStreamReader r = null;
        BufferedReader bf = null;

        try {
            //测试文件地址
            fs = new FileInputStream("D:/tsxk.txt");
            //中文一般编码方式UTF-8或者GBK
            r = new InputStreamReader(fs, "GBK");
            bf = new BufferedReader(r);

            //扫描器,用于识别用户回车操作
            Scanner input = new Scanner(System.in);
            //书签-值为文本(书签所在行忽略)
            String shuQian="第二十四章";
            int maxLinLenght = 60;  //每行最大长度
            int len = 20;            //篇幅长度

            String in;

            String content;
            String sqLine;
            //书签操作-书签所在行不读取
            boolean go = true;
            do {
                content = bf.readLine();
                if(content!=null){
                    go = !content.contains(shuQian);
                }
            }while(go);
            //书签操作-结束

            do {
                System.out.println("\n\n\n\n\n\n\n\n\n\n");
                for (int i = 0; i < len ;i++){
                    if((content=bf.readLine())!=null){
                        if("".equals(content)){
                            //补全一行,循环增加一次
                            i--;
                            continue;
                        }
                        //拆分过长一行
                        if (content.length() > maxLinLenght) {
                            int lineNum = content.length() / maxLinLenght + 1;
                            String[] str = new String[lineNum];
                            //循环截取数组
                            for (int j = 0; j < lineNum; j++) {
                                //判断末端下标位置
                                int endIndex = content.length();
                                if (content.length() > (j + 1) * maxLinLenght) {
                                    endIndex = (j + 1) * maxLinLenght;
                                }

                                str[j] = content.substring(j * maxLinLenght, endIndex);
                                System.out.println(str[j]);
                            }
                        } else {
                            System.out.println(content);
                        }
                    }
                }
                //输入回车进入下一篇
                in = input.nextLine();
            }while ("".equals(in));
            System.out.println("=====结束操作=====");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fs != null) {
                    fs.close();
                }
                if (r != null) {
                    r.close();
                }
                if (bf != null) {
                    bf.close();
                }
            }catch (Exception e){
                System.out.println("关闭资源时报错!");
                e.printStackTrace();
            }
        }

    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ywh22122

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值