gpt4 book ai didi

java - Java自带的Transformer库将文件路径中的空格转换成%20

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:45:46 31 4
gpt4 key购买 nike

这是一个写出 XML 文件的测试应用程序。

为什么我的路径中的空格被转换为 %20

public class XmlTest
{
public static void main(String[] args)
{
String filename = "C:\\New Folder\\test.xml";
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
DOMSource source = new DOMSource(doc);

File xmlFile = new File(filename);
if (xmlFile.exists())
{
xmlFile.delete();
}
StreamResult result = new StreamResult(xmlFile);
transformer.transform(source, result);

}
catch (ParserConfigurationException ex)
{
ex.printStackTrace();
}
catch (TransformerException tfe)
{
tfe.printStackTrace();
}
}
}

堆栈跟踪:

java.io.FileNotFoundException: C:\New%20Folder\test.xml (The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
at org.apache.xalan.transformer.TransformerIdentityImpl.createResultContentHandler(TransformerIdentityImpl.java:287)
at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:330)
at avm.trans.xml.XmlTest.main(XmlTest.java:52)

最佳答案

尝试改变这一行:

StreamResult result = new StreamResult(xmlFile);

进入这个:

StreamResult result = new StreamResult(new FileOutputStream(xmlFile));

我不知道为什么将文件名作为 URL 处理。

关于java - Java自带的Transformer库将文件路径中的空格转换成%20,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20434510/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com