Java Application的Java文件路径转义字符?

时间:2016-09-08 13:54:10

标签: java linux properties escaping filepath

在我的Java应用程序中,我有一个属性Class(如下所示)。此类引用Linux Server上的application.properties文件。

输入文件路径的正确形式是什么,我需要Escape Chars吗?

当前属性类:

**
 * Properties class used to obtain properties from the
 * application.properties file
 */
public class MyProperties {

    private static Properties props = new Properties();
    static {
        try {
           //path to file on server
            java.io.InputStream in= new FileInputStream("/path/to/file/application.properties");
            props.load(in);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Returns a given property by its key
     * @param key
     * @return
     */
    public static String getProperty(String key) {

        return props.getProperty(key);
    }

}

我应该使用://path//to//file//application.properties而不是我目前的实施吗?

1 个答案:

答案 0 :(得分:0)

如果您使用单引号,则无需转义任何内容。 它将逃脱在shell模式下转义的所有内容。

尝试使用:

java.io.InputStream in= new FileInputStream('/path/to/file/application.properties');

根据您的评论,我添加了指向属性文件的链接:

Reading Java Properties file without escaping values

也许它完全符合你的需要