有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用jar文件xmlparserv2时使用java。jar,当文件路径有空格时,转换a不起作用。我该怎么解决这个问题?

通常在使用

Transformer t = TransformerFactory.newInstance().newTransformer(); 
t.transform(source,result);

(没有xmlparserv2.jar文件)未找到文件异常如下所示

Exception in thread "main" java.io.FileNotFoundException: C:\Documents and Settings\username\nonExistentFile.xml (The system cannot find the file specified)

当包含xmlparserv2时。jar,例外情况是这样的

Caused by: java.io.FileNotFoundException: C:\Documents%20and%20Settings\username\existingFile.xml (The system cannot find the path specified)

文件实际上就在那里(当我不包含jar时,transform方法会找到它),但是当我包含jar时,transform方法无法找到它,因为插入了%20作为空白。谁能告诉我怎么解决这个问题吗


共 (1) 个答案

  1. # 1 楼答案

    抱歉,应该包含更多代码。。。答案在我传递的结果对象中

    原来是这样的

    File f = new File(filePath);
    Result result = new StreamResult(f); 
    

    改成这个

    File f = new File(filePath);
    StreamResult result = new StreamResult(); 
    FileOutputStream fos = new FileOutputStream(f);
    result.setOutputStream(fos);