首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何删除java中的文件路径编码%20

如何删除java中的文件路径编码%20
EN

Stack Overflow用户
提问于 2017-04-22 20:46:25
回答 1查看 3.4K关注 0票数 4

我正在尝试使用java在特定路径中创建xml文件。问题是,如果我给出有空格的文件路径,则在空格中使用'%20‘进行编码。请帮我解决这个问题。

我给出的文件路径- "F:/Backup /testng2.xml“

编码后-“F:/Backup%20 20Files/testng2.xml”

代码:

代码语言:javascript
复制
 TransformerFactory transformerFactory = TransformerFactory.newInstance();
 Transformer transformer = transformerFactory.newTransformer();
 DOMSource source = new DOMSource(doc);
 StreamResult result = new StreamResult(new File("F:/Backup Files/testng2.xml"));       
 transformer.transform(source, result);
 System.out.println("File saved successfully");

错误:

代码语言:javascript
复制
javax.xml.transform.TransformerException: java.io.FileNotFoundException: F:\Backup%20Files\testng2.xml (The system cannot find the path specified)
    at org.apache.xalan.transformer.TransformerIdentityImpl.createResultContentHandler(TransformerIdentityImpl.java:297)
    at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:330)
    at FrameworkKeywords.ConfigurationFunctions.generateTestngXmlFile(ConfigurationFunctions.java:87)
    at FrameworkKeywords.ConfigurationFunctions.main(ConfigurationFunctions.java:29)
Caused by: java.io.FileNotFoundException: F:\Backup%20Files\testng2.xml (The system cannot find the path specified)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at org.apache.xalan.transformer.TransformerIdentityImpl.createResultContentHandler(TransformerIdentityImpl.java:287)
    ... 3 more
---------
java.io.FileNotFoundException: F:\Backup%20Files\testng2.xml (The system cannot find the path specified)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at org.apache.xalan.transformer.TransformerIdentityImpl.createResultContentHandler(TransformerIdentityImpl.java:287)
    at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:330)
    at FrameworkKeywords.ConfigurationFunctions.generateTestngXmlFile(ConfigurationFunctions.java:87)
    at FrameworkKeywords.ConfigurationFunctions.main(ConfigurationFunctions.java:29)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-22 21:12:01

尝试将.getAbsolutePath()添加到File中,如下所示:

代码语言:javascript
复制
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File("F:/Backup Files/testng2.xml").getAbsolutePath());        
    transformer.transform(source, result);
    System.out.println("File saved successfully");
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43564430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档