0

我已经使用 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 能再次帮助我。

4

3 回答 3

0

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

于 2012-10-26T12:16:02.880 回答
0

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

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

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

于 2012-10-26T12:35:25.120 回答
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();
}

谢谢您的帮助。

于 2012-10-29T08:25:47.830 回答