Java 使用FileWriter覆盖同一文件

Java 使用FileWriter覆盖同一文件,java,file,filewriter,Java,File,Filewriter,我想覆盖同一个文件。我在GUI中有一些文本字段作为输入,当我单击按钮(JButton)时,它应该将其写入文件。如果我再次单击它,它将覆盖内容。当前,只有在关闭应用程序并再次打开后单击按钮时,才会覆盖它,但当我在同一会话中多次单击时,它会附加它而不是覆盖。请查找我尝试的以下代码 btnSetupProcess.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e)

我想覆盖同一个文件。我在GUI中有一些文本字段作为输入,当我单击按钮(JButton)时,它应该将其写入文件。如果我再次单击它,它将覆盖内容。当前,只有在关闭应用程序并再次打开后单击按钮时,才会覆盖它,但当我在同一会话中多次单击时,它会附加它而不是覆盖。请查找我尝试的以下代码

btnSetupProcess.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String batchFilePath = createBatchFileAndGetPath(); 
            }
        });

batchFileContent
来自哪里?有可能内容被附加到变量本身吗?谢谢,我犯了一个愚蠢的错误:(它来自StringBuilder,我没有再次初始化它)。
private String createBatchFileAndGetPath() {
File newFile = new File("Test.bat");
            try(FileWriter writer = new FileWriter(newFile, false))  {
                //newFile.createNewFile();
                writer.write( batchFileContent.toString());
                return newFile.getAbsolutePath();
            } catch (IOException ex) {
                ex.printStackTrace();

return "";          }
}