title: How to overwrite an existing .txt file
body: <p>I have an application that creates a .txt file. I want to overwrite it. This is my function:</p>
  
  <pre><code>try{
      String test = "Test string !";
      File file = new File("src\\homeautomation\\data\\RoomData.txt");
  
      // if file doesnt exists, then create it
      if (!file.exists()) {
          file.createNewFile();
      }else{
  
      }
  
      FileWriter fw = new FileWriter(file.getAbsoluteFile());
      BufferedWriter bw = new BufferedWriter(fw);
      bw.write(test);
      bw.close();
  
      System.out.println("Done");
  }catch(IOException e){
      e.printStackTrace();
  }
  </code></pre>
  
  <p>What should I put in the else clause, if the file exists, so it can be overwritten?</p>