2013-03-21 70 views
-1

我有一個調用路徑並且位置被硬編碼的xml。在xml中調用.properties的路徑

<path=c:/... /> 

有誰知道如何使用.properties放入路徑,然後從那裏調用它? 我出於安全原因這樣做。

編輯

繼教程 http://www.mulesoft.org/documentation/display/current/Basic+Studio+Tutorial

入站的路徑被稱爲如下:

<file:inbound-endpoint path="C: ../> 

能把它從屬性文件中加載?

+0

你能更清楚嗎? – abhinav 2013-03-21 15:05:14

+0

pease添加一些示例代碼,以便我們可以更好地理解您的意思。 – MemLeak 2013-03-21 15:05:15

回答

0

代碼加載propertie文件是:

Properties prop = new Properties(); 
    prop.load(new FileInputStream("C:\\prop.properties")); 
    String path = prop.getProperty("pathname"); 

但你有硬編碼路徑propertie文件: 你可以只通過作爲主要方法的arg的路徑:

public static void main(String Args[]){ 
    try { 
     if(Args != null){ 
      if(Args[1] != null){//Load XML or prop File     try{ 
        properties.load(new FileInputStream(Args[1])); 
       }catch(Exception e){ 
        throw new Exception("Error loading PropertieFile: "+Args[1] + " :"+e.getMessage()); 
       } 
      }else{ 
       throw new Exception("No File Found!"); 
      } 
0
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> 
<properties> 
    <comment>Application Configuration</comment> 
    <entry key="data.folder">D:\App\Data</entry> 
    <entry key="jdbc.url">jdbc:mysql://localhost/mydb</entry> 
</properties> 

LoadXmlProperties的.java

import java.io.FileInputStream; 
import java.util.Properties; 

public class LoadXmlProperties { 
    public static void main(String[] args) { 
     LoadXmlProperties lxp = new LoadXmlProperties(); 
     try { 
      Properties properties = lxp.readProperties(); 
      /* 
      * Display all properties information 
      */ 
      properties.list(System.out); 

      /* 
      * Read the value of data.folder and jdbc.url configuration 
      */ 
      String dataFolder = properties.getProperty("data.folder"); 
      System.out.println("dataFolder = " + dataFolder); 
      String jdbcUrl = properties.getProperty("jdbc.url"); 
      System.out.println("jdbcUrl = " + jdbcUrl); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public Properties readProperties() throws Exception { 
     Properties properties = new Properties(); 
     FileInputStream fis = new FileInputStream("configuration.xml"); 
     properties.loadFromXML(fis); 

     return properties; 
    } 
}