title: Unable to write to the file, Created in java using File class
body: <p>Class <code>MakeDirectory</code> contains the constructor, and in the constructor I created a directory and inside that directory I created a file. But I am unable to write anything to the newly created file, even though the file and directory have been generated successfully. Can anyone help me figure out why I am not able to write to the file Anything.txt?</p>
  
  <pre><code>public class MakeDirectory {
      MakeDirectory() throws IOException{
          // Creates Directory
          File mydir= new File("MyDir");
          mydir.mkdir();
  
          // Creates new file object
          File myfile = new File("MyDir","Anyfile.txt");
  
          //Create actual file Anyfile.txt inside the directory
          PrintWriter pr= new PrintWriter(myfile);
          pr.write("This file is created through java");
      }
  
      public static void main(String args[]) throws IOException {
          new MakeDirectory();
      }
  }
  </code></pre>