在我的 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/