2 回答
TA贡献1820条经验 获得超10个赞
你不需要第一个FileWriter fileWriter = new FileWriter("Sorted output.txt");;这实际上是创建/覆盖文件,之后您的 PrintWriter 再次打开它以进行附加。
所以,只要改变
// ... omitting beginning
try {
FileWriter fileWriter = new FileWriter ("Sorted output.txt");
//BufferedWriter bufferedWriter = new BufferedWriter (fileWriter);
PrintWriter out = new PrintWriter (new FileWriter("Sorted output.txt", true));
do {
// ... omitting rest
至
// ... omitting beginning
try {
//BufferedWriter bufferedWriter = new BufferedWriter (fileWriter);
PrintWriter out = new PrintWriter (new FileWriter("Sorted output.txt", true));
do {
// ... omitting rest
TA贡献1851条经验 获得超5个赞
将 out.close() 移出循环
else {
System.out.println("Please enter a valid option i.e. 1,2 or 3");
tInput.next();
}
out.flush();
/* THIS -> out.close(); <- THIS */
//tInput.close();
}while (userSelection != optionThree);
out.close();
}
我试过你的代码,你的问题不是文件被覆盖,而是你在第一次迭代中关闭了输出流。
添加回答
举报