太无聊了怎么办?从编译器控制台看会书吧

这个博客介绍了一个简单的Java程序,它能够读取文本文件并允许用户通过控制台进行阅读。程序支持从上次保存的书签位置继续阅读,并且可以保存新的书签行号。用户只需输入行号,即可在控制台查看对应行的内容。程序无需任何外部库,完全依赖JDK。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

只需要jdk,不需要任何依赖库!

利用编译器控制台即可进行阅读!

并且看起来无疑是在调试代码,找bug!

效果图:

在这里插入图片描述

步骤1:将代码复制一下。
步骤2:配置开头的bookSrc参数,即书籍的路径
步骤3:运行代码
步骤4:将控制台翻回最上方,开始阅读
步骤5:保存标签:每一行最左边显示行数,直接在控制台输入该数字。

直接上代码:

import java.io.*;
import java.util.Scanner;

public class CDBook {
    //书籍路径
    final static String bookSrc = "D:\\dpcq.txt";

    private static int readLabel() {
        int labelLine = 0;
        File file = new File(bookSrc + ".label");
        if (file.exists()) {
            try (FileReader label = new FileReader(file)) {
                BufferedReader bufferedReader2 = new BufferedReader(label);
                String line = bufferedReader2.readLine();
                int num = Integer.parseInt(line);
                System.out.println("上次读到第" + num + "行。   保存书签:在控制台输入阅读行最左侧的行数,按回车");
                labelLine = num;
                bufferedReader2.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return labelLine;
    }

    private static void readBook(int readline) {
        try (FileReader bookReader = new FileReader(bookSrc)) {
            BufferedReader bufferedBookReader = new BufferedReader(bookReader);
            String line = bufferedBookReader.readLine();
            int number = 0;
            while (line != null) {
                if (number<=readline+1000 && number >= readline) {
                    System.out.println(number + "|" + line);
                    System.out.println("A problem occurred evaluating root project ''.\n" +
                            "> Failed to apply plugin [id 'cloud']");
                }
                line = bufferedBookReader.readLine();
                number++;
            }
            System.out.println("已阅读1000行,请保存书签:在控制台输入阅读行最左侧的行数,按回车");
            System.out.println("加载完毕,请将控制台上划到最上方开始阅读!!!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        int readline;

        //读书签
        readline = readLabel();

        //从指定书签处继续读书
        readBook(readline);

        Scanner scanner = new Scanner(System.in);
        int labelLine = scanner.nextInt();
        try {
            File destFile = new File(bookSrc + ".label");
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(destFile)); //存为UTF-8
            osw.write(labelLine + "");
            //刷新缓冲区的数据,强制写入目标文件
            osw.flush();
            osw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("书签:保存到第" + labelLine + "行");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值