为什么我不能将我的答案添加到 readwrite.txt。?

why I can't add my answer to readwrite.txt.?

我尝试将我的答案添加到 readwrite.txt,但我每次 运行 代码时都找不到我的答案。

public static void main(String[] args) {

ArrayList<String> category1=new ArrayList<>();
category1.add("die hard");
category1.add("7.5");
category1.add("mission impossible");
category1.add("8");
category1.add("the expendabels");
category1.add("6");
ArrayList<String> category2=new ArrayList<>();
category2.add("the mask");
category2.add("due date");
ArrayList<String>userchoices=new ArrayList<>();
Scanner s= new Scanner(System.in);
System.out.println("select a movie");
String answer=s.nextLine();

String writepath="files/readwrite.txt";

try {
FileWriter writer=new FileWriter(writepath);
writer.write(answer);
} 
catch (FileNotFoundException e) {
e.printStackTrace();
} 
catch (IOException e) {
e.printStackTrace();
}

if (category1.contains(answer)) {
System.out.println(category1.get(category1.indexOf(answer) + 1) +
(" of 10"));
System.out.println("action");
}
if (answer.equals("action"))
System.out.println(category1);
else if (category2.contains(answer))
System.out.println("comedy");
if (answer.equals("comedy"))
System.out.println(category2);


}

}

我尝试将我的答案添加到 readwrite.txt,但我每次 运行 代码时都找不到我的答案。

完成写入文件后,您需要关闭 FileWriter。当您调用 close() 时,writer 会将其内部缓冲区刷新到磁盘,您会看到文本出现在文件中。当你不需要它们时,必须关闭任何资源。