This discussion is archived
7 Replies Latest reply: Jul 15, 2011 3:28 PM by EJP RSS

java.io.FileNotFoundException: (Access is denied) exeception

875776 Newbie
Currently Being Moderated
When I used to run this sample program I am getting java.io.FileNotFoundException: (Access is denied) exeception :

Sample Program : -
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sampleprogram;

import com.sun.org.apache.xerces.internal.dom.DocumentImpl;
import com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl;
import com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl;
import java.io.File;
import java.io.IOException;
import javax.swing.JOptionPane;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
 *
 * @author Admin
 */
public class MainStartClass {

    public MainStartClass() {
        String sFilePath = "C:" + System.getProperty("file.separator") + "ProgramData" + System.getProperty("file.separator") + ".xyz" + System.getProperty("file.separator") + "config" + System.getProperty("file.separator") + "xyz.xml";
        DocumentBuilderFactoryImpl factory = (DocumentBuilderFactoryImpl) DocumentBuilderFactory.newInstance();
        DocumentImpl doc = null;
        DocumentBuilderImpl parser = null;

        try {
            parser = (DocumentBuilderImpl) factory.newDocumentBuilder();
        } catch (ParserConfigurationException ex) {
            JOptionPane.showMessageDialog(null, ex.toString());
            return;
        }
        try {
            doc = (DocumentImpl) parser.parse(new File(sFilePath));
        } catch (SAXException ex) {
            JOptionPane.showMessageDialog(null, ex.toString());
            return;
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, ex.toString());
            return;
        }
        try {
            saveDocument(doc, sFilePath);
            System.out.println("8");
        } catch (TransformerFactoryConfigurationError ex) {
            JOptionPane.showMessageDialog(null, ex.toString());
            return;
        } catch (TransformerException ex) {
            JOptionPane.showMessageDialog(null, ex.toString());
            return;
        }
        System.out.println("9 - finished");

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new MainStartClass();
    }

    public static synchronized void saveDocument(Document document, String sFilePath) throws TransformerFactoryConfigurationError, TransformerException {
        synchronized (sFilePath) {
            try {
                System.out.println("1");
                Transformer transformer = TransformerFactory.newInstance().newTransformer();
                System.out.println("2");
                DOMSource xmlSource = new DOMSource(document);
                System.out.println("3");
                transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                transformer.setOutputProperty(OutputKeys.VERSION, "1.0");
                transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
                System.out.println("4");
                StreamResult result;

                File f = new File(sFilePath);
                System.out.println("5");
                if (f.exists() == false) {
                    System.out.println("5.1");
                    result = new StreamResult(f);
                } else {
                    System.out.println("5.2");
                    result = new StreamResult(sFilePath);
                }
                System.out.println("6");
                transformer.transform(xmlSource, result);
                System.out.println("7");

            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex.toString());
                return;
            }
        }
    }
}
*Exception : --*
java.io.FileNotFoundException: C:\ProgramData.\config\.xml (Access is denied) stacktrace javax.xml.transform.TransformerException: java.io.FileNotFoundException: C:\ProgramData.\config\.xml (Access is denied)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getOutputHandler(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source) at com..main.dr.a(Unknown Source)
at com..main.dr.a(Unknown Source) at com..main.dr.a(Unknown Source)
at com..main.h.k(Unknown Source)
at com..main.ay.run(Unknown Source) Caused by: java.io.FileNotFoundException: C:\ProgramData.\config\.xml (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream. (Unknown Source)
at java.io.FileOutputStream. (Unknown Source)
... 7 more
--------- java.io.FileNotFoundException: C:\ProgramData.\config\.xml (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream. (Unknown Source)
at java.io.FileOutputStream. (Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getOutputHandler(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
at com..main.dr.a(Unknown Source)
at com..main.dr.a(Unknown Source)
at com..main.dr.a(Unknown Source)
at com..main.h.k(Unknown Source)
at com..main.ay.run(Unknown Source)
and the cause is
java.io.FileNotFoundException: C:\ProgramData.\config\.xml (Access is denied)
Edited by: EJP on 15/07/2011 22:31: added {noformat}
{noformat} tags. Please use them so your code is actually readable.

Edited by: 872773 on Jul 15, 2011 5:53 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points