我正在尝试使用“ new XmlNodePrinter(new PrintWriter(new FileWriter(filepath)))。print(xmlParse)”更新和保存现有XML文件,每次我在读取文件“ java.io”时遇到错误。 FileNotFoundException :(权限被拒绝)”。
我的代码是:
def filepath = "X:/XXX/XX/abc.xml"
def xml = readFile(filepath)
def xmlParse = new XmlParser().parseText(xml)
xmlParse.project.each { A ->
if (A.@name == XXXXX){
A.Branch.each { B ->
if (B.@id == XXXXX){
Do somthing....
B.@id = YYYY
}
}
new XmlNodePrinter(new PrintWriter(new FileWriter(filepath))).print(xmlParse)
错误:
java.io.FileNotFoundException: X:\XXX\XX\abc.xml (Permission denied)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
at java.io.FileWriter.<init>(FileWriter.java:63)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:105)