将系统 属性 设置为文件路径

Setting system property to a file path

我正在尝试将 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(),它将 return false 首先检查生成的路径。