java - Java 应用程序的 Linux 文件路径转义字符?

标签 java linux properties escaping filepath

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

最佳答案

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

尝试使用:

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

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

Reading Java Properties file without escaping values

也许它完全符合你的需要

关于java - Java 应用程序的 Linux 文件路径转义字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39392848/

相关文章:

尽管能够访问数组,Java 列表监听器仍会抛出空指针错误

java - 在 Java 中生成所有排列

java - JPanel 被传递为 null

linux - 有没有办法告诉哪个编译二进制文件的主机名正在使用 gdb?

class - 复制 Groovy 类属性

svn 提交 : to which "--encoding" parameter applies?

关于 Primefaces 中托管用法的 Java 类名称约定

linux - 如何在 linux 机器上以 root 用户身份运行 Elasticsearch 5.2.1

python - 在 Linux 上使用 discord.py 我得到错误 'Bot' object has no attribute 'join_voice_channel'

c# - 在C#类项目中,AssemblyCulture是干什么用的?