gpt4 book ai didi

java - 从文本文件中读取一行而不是整个文件(使用 bufferedreader)

转载 作者:行者123 更新时间:2023-12-01 17:51:14 25 4
gpt4 key购买 nike

我正在尝试编写一段代码,使用缓冲读取器从 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)

然后重复此操作,遍历文本文件中的每一行,直到到达文本文件的末尾(其中会输出类似“游戏完成”的内容)...

最佳答案

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

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

关于java - 从文本文件中读取一行而不是整个文件(使用 bufferedreader),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60792683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com