我正在尝试将 System.setProperty
设置为文件路径:
//properties key
String propFile = "propertiesFile";
String pathToFile = "properties/prop.properties";
File file = new file(pathToFile);
//properties value
String path = file.getAbsolutePath();
System.setProperty(propFile, path);
//using properties.....
我收到了FileNotFoundException
。
打印文件时,我得到 absolutePath
- c:\Project...\prop.properties
设置 System.setProperty 是否应该以另一种方式完成?
properties
- src
内的包。
最佳答案
您的问题与方法 System.setProperty
无关,因为您的路径像任何其他 String
一样进行管理,您的问题不仅仅是 new File(pathToFile )
指的是不存在的文件,因为您提供相对路径,而绝对路径是从用户目录(System.getProperty("user.dir")
的值)创建的,即可能不是你所期望的。如果您调用 new File(pathToFile).exists()
,它将返回 false
首先检查结果路径。
关于java - 将系统属性设置为文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37655831/