Q:

Cant Write to file, after creating it (and its directory) in java

I am trying to download a XML file from the internet, and then place this file in a directory (C:/Nationstates).
File theDir = new File("/NationStates");

  if (!theDir.exists()) {
    System.out.println("creating directory: /NationStates" );
    boolean result = false;

    try{
        theDir.mkdir();
        result = true;
     } catch(SecurityException se){
       System.out.println("Dir exists");
     }        
  }

  new PrintWriter("/NationStates/NS.xml");

  URL website = new URL("https://www.nationstates.net/cgi-bin/api.cgi?nation=ageena");
  ReadableByteChannel rbc = Channels.newChannel(website.openStream());
  FileOutputStream fos = new FileOutputStream("/NS.xml");
  fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
  fos.close();

I can create the directory, and create the file (NS.XML) but when I attempt to write to the file I get the following errors:
Exception in thread "main" java.io.FileNotFoundException: \NS.xml (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at XML.main(XML.java:31)

It says access denied, but I just created a directory and a file in that directory in the same location. 
Any ideas how to fix this?

A:

You create a directory:
File theDir = new File("/NationStates");
...
theDir.mkdir();

But then you don't write to a file in this directory:
FileOutputStream fos = new FileOutputStream("/NS.xml");

Moreover, mkdir() doesn't necessarily create the directory. If it fails, it returns false. But you don't check the returned value.

A:

replace :
FileOutputStream fos = new FileOutputStream("/NS.xml");

by
FileOutputStream fos = new FileOutputStream("/NationStates/NS.xml");

