access right to parse an XML in java

2019-08-21 08:37发布

问题:

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")));