2017-04-22 10 views
1

javaを使用して特定のパスにxmlファイルを作成します。 スペースでファイルパスを指定すると、空白スペースに「%20」とエンコードされています。 この問題を解決するのを助けてください。私が与えられたJavaでファイルパスのエンコーディング%20を削除するには

ファイルパス - "F:/バックアップファイル/ 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) 

答えて

1

てみてください、次のように、あなたのFile.getAbsolutePath()を追加:

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

ありがとう@Plirkee。あなたの答えは非常に便利です。それは私のために働いています。 – Baskar

関連する問題