1

So I'm trying to create an XML File in my C:\ directory and every time I launch the program, it says Access Is Denied. The following is my code:

public void buildXML(){
    try {
        DocumentBuilderFactory xmlFac = DocumentBuilderFactory.newInstance();
        DocumentBuilder doc = xmlFac.newDocumentBuilder();

        Document settings = doc.newDocument();
        Element rootElement = settings.createElement("AppSettings");

        rootElement.appendChild(settings.createElement("FAK0"));

        TransformerFactory transform = TransformerFactory.newInstance();
        Transformer former = transform.newTransformer();
        DOMSource src = new DOMSource(settings);
        StreamResult res = new StreamResult(new File("C:\\file.xml"));

        former.transform(src, res);
    }catch(Exception e){
        System.out.println(e+"");
    }
}

And here is the output on the console:

javax.xml.transform.TransformerException: java.io.FileNotFoundException: C:\file.xml (Access is denied)

Is it because I have no administrator access or what? If it is then that would be very unsettling.

5
  • Yeah it is more likely that its because of permission issues. Try to create the file in the same location as your java file to confirm the issue. Commented Jul 26, 2014 at 18:49
  • Do you absolutely must create the file on C:? On any other partition it should work. Commented Jul 26, 2014 at 18:51
  • @Fyre it works with my current directory Commented Jul 26, 2014 at 18:52
  • @GabrielNegut I actually need it to be in the Windows Directory, so yeah. Commented Jul 26, 2014 at 18:52
  • Then this is permission issue. You dont have the access to write the C drive. You could try to open your cmd as administrator and run. Commented Jul 26, 2014 at 18:53

1 Answer 1

1

2 things:

  1. Perhaps the file is open and edited by another program
  2. Or as the others have suggested about permissions.

Check to see if a file.xml exists in that location.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.