title: Buffered Writer overwriting file when not wanted
body: <p>I have this code here that takes in 3 arguments, A Directory, a Filename, and a number. The program creates the filename in the directory and writes the number in it. So I can say...</p>
  
  <pre><code>&gt;java D: myName.txt Clay 100
  </code></pre>
  
  <p>which will create a file named myName.txt in D: and says 100 in it.</p>
  
  <p>If myName is taken up, it changes the name to myName(2), then myName(3) (if myName(2) taken up). The only problem is that when it changes the name to myName(2) and writes, it overwrites myName. I dont want it to overwrite myName, I want it to just create a new file with that name. Ive looked at similar questions and the common answer is the flush and close the writer which ive done And it still doesnt work. </p>
  
  <p>Any help would be appreciated, here is my code so fart...</p>
  
  <pre><code>import java.io.*;
  
  public class filetasktest{
    public static void main(String[] args) throws IOException{
    int i = 2;
    String directory = args[0];
    if (directory.substring(directory.length() - 1) != "/"){
      directory += "/";
    }
    String contactName = args[1];
    String contactNumber = args[2];
  
    String finalDirectory = directory + contactName + ".contact";
  
    File f = new File(finalDirectory);
  
    while (f.exists()){
      finalDirectory = directory + contactName + "(" + ("" + i) + ")" + ".contact";
      f.renameTo(new File(finalDirectory));
      i++;
    }
  
    Writer writer = null;
    try {
      writer = new BufferedWriter(new OutputStreamWriter(
      new FileOutputStream(finalDirectory), "utf-8"));
      writer.write(contactNumber);
    } catch (IOException ex){
      System.out.println(ex.getMessage());
    } finally {
      try {
        writer.close();
      } catch (Exception ex) {
        System.out.println(ex.getMessage());
      }
    }
  }
  </code></pre>
  
  <p>}</p>