title: Check whether file exists in a particular directory by giving file name at run time and also writing to the file if it exists Java
body: <p>Want to check whether the file exists at a particular path at runtime or not by providing user input and write to it if it already exists and if doesn't it should show the appropriate message like couldn't write.</p>
  
  <pre><code>void writeFile(){
  
      System.out.println("Enter file name to write in");
      str = sc.next();
      File f1 = new File("path",str);
      if(f1.exists()){
          try{
              FileWriter fw = new FileWriter(f);
              System.out.println("Enter what to write");
              String str = sc.nextLine();
              fw.write(str);
              fw.flush();
              System.out.println("Written inside file" + f);
          }
          catch(IOException e)
          {
              System.out.println("issue");
          }
  }
  }
  </code></pre>