【发布时间】:2023-03-14 09:35:01
【问题描述】:
这是我的代码。如果我输入 3 行文本然后键入“exit”,它将仅写入文件的最后一行文本。 我错过了什么? 谢谢。
`public class WriteFile {
public static void writeFile() throws IOException {
Scanner sc = new Scanner(System.in);
String text;
File file = new File("file.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(file));
while (!sc.nextLine().equalsIgnoreCase("exit")) {
text = sc.nextLine();
out.write(text);
out.newLine();
}
sc.close();
out.flush();
out.close();
}
}`
【问题讨论】:
标签: bufferedwriter writetofile