-1

我有一个调用路径的 xml,并且位置是硬编码的。

<path=c:/... />

有谁知道如何使用 .properties 放入路径然后从那里调用它?我这样做是出于安全原因。

编辑

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

入站的路径调用如下:

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

它可以从属性文件中加载吗?

4

2 回答 2

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>

加载XmlProperties .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;
    }
}
于 2013-03-21T15:09:39.063 回答
0

加载属性文件的代码是:

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

但是你有属性文件的硬编码路径:你可以将路径作为主要方法的 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!");
            }
于 2013-03-21T15:12:19.707 回答