access right to parse an XML in java

假装没事ソ 提交于 2019-12-25 08:40:34

问题


hello i have to execute this command:

    docRules = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse( new File(IeplcDeployRules.clx));

When i try to create a parser i get the following error:

java.io.FileNotFoundException: /.../IeplcDeployRules.clx (Permission denied)

if i try manually to read the file it works but i cannot write in it because following are the permission.

-rwxr-xr-x 1 ieplcop ieplcdev  3424 Aug 11 17:16 IeplcDeployRules.clx

I do not want to change the permission since the file NEED only to be read from my Java application. I suppose there shold be therefore a way to specify that the file should be opened in read only mode.

I look the possible paramenter of File() parse() and .newDocumentBuilder() but none of them let me specify that the operation is read only!!

Any idea about how to procede?

Cheers, Ste


回答1:


I do not know what the DocumentBuilder does internally, I would have to read its source. But you can use a different InputSource instead of a File, so that you have full control. For example a FileReader.

documentBuilder.parse(new InputSource(new FileReader(...));



回答2:


You should pass an InputStream to the parse method instead of a File.

For example:

docRules = DocumentBuilderFactory.newInstance()
    .newDocumentBuilder().parse( new FileInputStream(new File("IeplcDeployRules.clx")));


来源:https://stackoverflow.com/questions/7029007/access-right-to-parse-an-xml-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!