gpt4 book ai didi

Java:附加文本文件时跳过用户输入的第一行?

转载 作者:行者123 更新时间:2023-11-30 03:03:50 24 4
gpt4 key购买 nike

所以我试图接受用户的文本行,以便将其附加到 txt 文件中。但是,程序运行后,用户输入的第一行不会出现在附加文件中。

这是部分代码:

System.out.println("enter advice (hit return  on empty line to quit):");
String advice = keyboard.nextLine();

FileWriter fw = new FileWriter(inputFile, true);
PrintWriter pw = new PrintWriter(fw);

for(int n = 1; n <= 2; ++n)
{
advice = keyboard.nextLine();
pw.print(advice);
}

pw.close();
keyboard.close();

这是代码的示例运行:

$ java Advice
enter input filename: Advice.txt **enter**
1: fully understand the given problem
2: do analysis and design first before typing
enter advice (hit return on empty line to quit):
a **enter**
b **enter**

Thank you, goodbye!
$ cat Advice.txt
1: fully understand the given problem
2: do analysis and design first before typing
b

有什么想法吗?谢谢!

最佳答案

用户输入的第一行将被捕获,但不会写入文件。字符串建议=keyboard.nextLine();当您写入文件时,您将接受下一个用户输入

关于Java:附加文本文件时跳过用户输入的第一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35302017/

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