title: How do I turn off overwrite warning for JavaFX FileChooser when appending to a file?
body: <p>I need to create a data-entry JavaFx class which outputs to a file in append mode. In the simplified runnable example below (imports deleted), I can create a file, put in initial data, and then append to it with no problem. However, if I close the program and run it again later, the FileChooser will help find the file, but warns:
  "Confirm Save As __
  TryFile.txt already exists. __
  Do you want to replace it?". </p>
  
  <p>If I choose to confirm, the new data is appended as intended and the file is not replaced. 
  How do I turn off that warning? I am using Java 8_20 on Windows 7.</p>
  
  <p>Note: I have corrected the code as per suggestions below. See AppendToFile().</p>
  
  <pre><code>public class TryAppend extends Application
  {
    static File file;
  
    static void initiatefile( String setupInfo )
    {
      FileChooser choose = new FileChooser();
      choose.getExtensionFilters().add(
              new FileChooser.ExtensionFilter( "Text doc(*.txt)", "*.txt" ) );
      choose.setInitialFileName( "*.txt" );
      choose.setInitialDirectory( new File(System.getProperty("user.home") ) );
      file = choose.showSaveDialog( null );
      if ( file != null )
      {
          if ( file.getName().endsWith( ".txt" ) )
          {
             try( PrintWriter out = new PrintWriter( new BufferedWriter( new FileWriter( file )))) 
             {
                 out.println( setupInfo );
             }catch (IOException e) {
                 System.err.println(e);
             }
          }
      }
    }
  
    static void appendToFile( String appendString )
    {
      if (file == null)
      {
          FileChooser choose = new FileChooser();
          choose.getExtensionFilters().add(
                  new FileChooser.ExtensionFilter( "Text doc(*.txt)", "*.txt" ) );
          choose.setInitialDirectory( new File(System.getProperty("user.home") ) );
          file = choose.showOpenDialog( null ); //Now corrected. Previously 
                                                //was: choose.showSaveDialog( null );                    
      }
  
      if ( file != null )
      {   
  
          try( PrintWriter out = new PrintWriter( new BufferedWriter( new FileWriter( file, true )))) 
          {
              out.println( appendString );
          }catch (IOException e) {
              System.err.println(e);
          }
      }
    }
  
    @Override
    public void start(Stage primaryStage) 
    { 
       primaryStage.setTitle( "Try Append to File" ); 
       Group root = new Group(); 
       Scene scene = new Scene( root, 300, 200 ); 
       Button initBtn = new Button(); 
       initBtn.setLayoutX( 100 ); 
       initBtn.setLayoutY( 40 ); 
       initBtn.setText( "Initiate File" ); 
       initBtn.setOnAction( new EventHandler&lt;ActionEvent&gt;() 
       { 
          public void handle(ActionEvent event) 
          { 
              initiatefile( "Initial data" ); 
          } 
       } ); 
       Button appendBtn = new Button(); 
       appendBtn.setLayoutX( 90 ); 
       appendBtn.setLayoutY( 100 ); 
       appendBtn.setText( "Append to File" ); 
       appendBtn.setOnAction( new EventHandler&lt;ActionEvent&gt;() 
       { 
          public void handle(ActionEvent event) 
          { 
              appendToFile( "Appended data" );
          } 
       } ); 
       root.getChildren().add( initBtn ); 
       root.getChildren().add( appendBtn ); 
       primaryStage.setScene( scene ); 
       primaryStage.show(); 
    }
    public static void main(String[] args) 
    {
      launch(args);
    }
  }
  </code></pre>