【问题标题】:Linux File path- escape characters for Java Application?Java应用程序的Linux文件路径转义字符?
【发布时间】:2016-09-08 13:54:09
【问题描述】:

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

输入文件路径的正确格式是什么,我需要转义字符吗?

当前属性类:

**
 * 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 而不是我当前的实现吗?

【问题讨论】:

    标签: java linux properties escaping filepath


    【解决方案1】:

    如果你使用单引号,你不需要转义任何东西。 它会转义所有需要在 shell 模式下转义的东西。

    尝试使用:

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

    根据您的评论,我添加了这个引用属性文件的链接:

    Reading Java Properties file without escaping values

    也许它完全符合您的需要

    【讨论】:

    • 好的,在框的属性文件上,我需要添加任何引号,还是只指定路径?
    • 请看链接。为了清楚起见,它被添加到答案中。
    • 我尝试使用单引号,但收到错误:“字符串文字中的字符太多”?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    • 2023-03-21
    • 2013-12-17
    • 2020-06-28
    • 2011-10-06
    • 1970-01-01
    相关资源
    最近更新 更多