这是我的项目结构:
我需要在MyClass.java
中读取config.properties
文件。我尝试使用相对路径如下所示:
// Code called from MyClass.java
File f1 = new File("..\\..\\..\\config.properties");
String path = f1.getPath();
prop.load(new FileInputStream(path));
这给了我以下错误:
..\..\..\config.properties (The system cannot find the file specified)
我如何在Java中定义相对路径?我使用的是jdk 1.6,并且在Windows上工作。
config.properties
文件放在src
目录下。在src
目录下创建一个名为config
的包,并将config.properties
文件放在config
包中。然后可以通过访问config/config.properties
来简单地获取它。 - Nandkumar Tekaleconfig.properties
会随着 Jar 文件一起分发吗?如果是这样的话,它将成为一个 [tag:embedded-resource],无法通过File
访问,而必须通过URL
访问。 - Andrew Thompson