编写新的xsd文件时出现FileNotFoundException

时间:2012-10-26 11:58:26

标签: java xml file xsd filenotfoundexception

我使用DOMparser解析了一批XML Schema文件。我添加了几个注释,这对我正在创建的应用程序至关重要。然后我想把这些新的“预处理”文件写到一个新位置,然后我得到一个FileNotFound异常(访问被拒绝)。

以下是我编写文件的代码片段:

Transformer tFormer = TransformerFactory.newInstance().newTransformer();

// Set output file to xml
tFormer.setOutputProperty(OutputKeys.METHOD, "xml");

// Write the document back to the file
Source source = new DOMSource(document);
File preprFile = new File(newPath(xmlFile));
    // The newPath function is a series of String operations that result in a new
    relative path

try {
    // Create file if it doesn't already exist;
    preprFile.mkdirs();
    preprFile.createNewFile();
} catch (Exception e) {
    e.printStackTrace();
}

Result result = new StreamResult(preprFile);
tFormer.transform(source, result);

我得到的错误如下:

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

上述代码段中的哪一行指向:

tFormer.transform(source, result);

我正在使用一台Windows机器(在某处读取可能是此错误的来源),我已经尝试关闭UAC,但没有成功。

我想也许createNewFile()方法在创建后不会释放文件,但无法找到有关该文件的更多信息。

希望StackOverflow能够再一次为我解救。

3 个答案:

答案 0 :(得分:0)

它可能在没有该目录权限的用户帐户下运行。

答案 1 :(得分:0)

您说“目录已创建,并且该文件也会创建为目录”。所以我认为它创建了名为'wsreportkbo_messages.xsd'的目录

它给你错误可能是因为你正在尝试读取目录。您可以使用 listFiles()列出目录中的文件。

您无法打开和阅读目录,使用 isFile() isDirectory()方法来区分文件和文件夹。

答案 2 :(得分:0)

我找到了解决方案:

File preprFile = new File(directory1/directory2/directory3/file.xsd);
File directory = new File(directory1/directory2/directory3/);

try {
// Create file if it doesn't already exist;
    directory.mkdirs();
    preprFile.createNewFile();                              
} catch (Exception e) {
    e.printStackTrace();
}

感谢您的帮助。

相关问题