java - 如何在 java 中删除文件路径编码 %20

标签 java xml windows filepath filenotfoundexception

我正在尝试使用 java 在特定路径中创建一个 xml 文件。 问题是,如果我给文件路径加上空格,它在空格中用 '%20' 编码。 请帮助我解决这个问题。

我给的文件路径——“F:/Backup Files/testng2.xml”
编码后——“F:/Backup%20Files/testng2.xml”

代码:

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

错误:

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)

最佳答案

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

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

关于java - 如何在 java 中删除文件路径编码 %20,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43564430/

相关文章:

javascript - Windows机器上网站的自动登录脚本?

java - 客户端无权调用此 JAX-RS EJB 错误

java - 如何从左移二进制和解释十六进制数字节数组?

java - 将 varchar 读取为 char 数组

javascript - 如何在 html 选项文本和值中插入 xml 文本和值

xml - 根据 .csv 文件更新 .xml 文档

c - 在 Windows 上格式化文件

java - 通过页面搜索已使用的资源并将其删除

xml - 如何使用 JAXB 抑制 XSI

ruby - Program Files 下的文件具有人格 split