从文本文件中读取一行而不是整个文件(使用缓冲 reader)

Read a single line from text file & not the entire file (using buffered reader)

我正在尝试编写一段代码,使用缓冲 reader 从 java 中的文本文件中读取一行文本。例如,代码将从文本文件中输出单行,然后您键入它所说的内容,然后它会输出下一行,依此类推。

到目前为止我的代码:

public class JavaApplication6 {

    public static String scannedrap;
    public static String scannedrapper;

    public static void main(String[] args) throws FileNotFoundException, IOException {
        File Tunes;
        Tunes = new File("E:\NEA/90sTunes.txt");

        System.out.println("Ready? Y/N");
        Scanner SnD;
        SnD = new Scanner(System.in);
        String QnA = SnD.nextLine();

        if (QnA.equals("y") || QnA.equals("Y")) {

            System.out.println("ok, starting game...\n");
            try {

                File f = new File("E:\NEA/90sTunes.txt");

                BufferedReader b = new BufferedReader(new FileReader(f));

                String readLine = "";

                while ((readLine = b.readLine()) != null) {
                    System.out.println(readLine);
                }

            } catch (IOException e) {
            }
        }
    }
}

它输出:

Ready? Y/N
y
ok, starting game...
(and then the whole text file)

但我希望实现这样的目标:

Ready? Y/N 
y
ok, starting game...
(first line of file outputted)
please enter (the line outputted)

& 然后重复此操作,遍历文本文件中的每一行,直到它到达文本文件的末尾(在那里它会输出类似 "game complete" 的内容)...

此代码块逐行读取整个文件,不会停下来请求用户输入:

        while ((readLine = b.readLine()) != null) {
            System.out.println(readLine);
        }

考虑向循环主体添加一条语句,以寻求用户的一些输入,就像您在上面询问他们是否准备好时所做的那样(您只需要向循环添加一行代码,例如分配的行QnA 的值)

这将读取第一行“.get(0)”。

String line0 = Files.readAllLines(Paths.get("enter_file_name.txt")).get(0);