如何使用带文件夹空间路径的java更新xml文件?

时间:2018-05-18 06:14:57

标签: java exception

String filepath = "C:/UIDriverProject/UIDriverPro/Test Folder/TESTDATA/TEST.dat";
DocumentBuilderFactory docFactory = 
DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filepath);

String filepath =" C:/ UIDriverProject / UIDriverPro / 测试文件夹 /TESTDATA/TEST.dat" ;;

我正在使用带有空格的文件路径 - "测试文件夹" 。如果我给文件夹没有空间它完美的工作,但与空间它说错误消息,如下面的错误消息。

  

javax.xml.transform.TransformerException中:   java.io.FileNotFoundException:   C:\ UIDriverProject \ UIDriverPro \ Test%20Folder \ TESTDATA \ TEST.dat(The   系统找不到指定的路径)   org.apache.xalan.transformer.TransformerIdentityImpl.createResultContentHandler(TransformerIdentityImpl.java:297)     在   org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:330)     在   org.apache.xalan.transformer.TransformerIdentityImpl $ $变换0.call(未知   来源)at   org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)     在   org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)     在   org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)     在helper.click_helper.updateXML(click_helper.groovy:148)at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)at   org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)     在groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)at   groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1215)at at   groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024)at at   groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:812)at at   groovy.lang.DelegatingMetaClass.invokeMethod(DelegatingMetaClass.java:144)     在helper.click_helper.invokeMethod(click_helper.groovy)at   com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:46)     在   org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.call(StaticMetaClassSite.java:53)     在   org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)     在   org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)     在   org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)     在Script1526547459389.run(Script1526547459389.groovy:30)at   com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:183)at at   com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:108)     在   com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:295)     在   com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:286)     在   com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:265)     在   com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:257)     在   com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:201)     在   com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:86)     在   com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:77)     在com.kms.katalon.core.main.TestCaseMain $ runTestCase $ 0.call(未知   来源)at   org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)     在   org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)     在   org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:149)     在TempTestCase1526623462047.run(TempTestCase1526623462047.groovy:20)     在   groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:263)     在groovy.lang.GroovyShell.run(GroovyShell.java:518)at at   groovy.lang.GroovyShell.run(GroovyShell.java:507)at   groovy.ui.GroovyMain.processOnce(GroovyMain.java:653)at at   groovy.ui.GroovyMain.run(GroovyMain.java:384)at   groovy.ui.GroovyMain.process(GroovyMain.java:370)at   groovy.ui.GroovyMain.processArgs(GroovyMain.java:129)at at   groovy.ui.GroovyMain.main(GroovyMain.java:109)at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)at   org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:109)     在   org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:131)   引起:java.io.FileNotFoundException:   C:\ UIDriverProject \ UIDriverPro \ Test%20Folder \ TESTDATA \ TEST.dat(The   系统找不到指定的路径)   java.io.FileOutputStream.open0(Native Method)at   java.io.FileOutputStream.open(FileOutputStream.java:270)at   java.io.FileOutputStream。(FileOutputStream.java:213)at   java.io.FileOutputStream。(FileOutputStream.java:101)at   org.apache.xalan.transformer.TransformerIdentityImpl.createResultContentHandler(TransformerIdentityImpl.java:287)     ... 51更多

2 个答案:

答案 0 :(得分:2)

你应该试试这个:

    String filepath = "C:/UIDriverProject/UIDriverPro/Test Folder/TESTDATA/TEST.dat";
    URI file = new File(filepath).toURI();
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(file.toURL().toString());

转换为URI对象可确保注意空白 URI file = new File(filepath).toURI();

答案 1 :(得分:-1)

请尝试将代码修改为以下内容,如果修复了该代码,请告诉我。

&nbsp