如何在Java中定义相对路径
我需要读config.properties里面MyClass.java。我尝试通过如下相对路径进行操作:
// Code called from MyClass.javaFile 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上工作。
回答:
试试这个
String filePath = new File("").getAbsolutePath();filePath.concat("path to the property file");
因此,新文件指向创建路径,通常是项目主文件夹。
[编辑]
正如@cmc所说,
    String basePath = new File("").getAbsolutePath();    System.out.println(basePath);
    String path = new File("src/main/resources/conf.properties")
                                                           .getAbsolutePath();
    System.out.println(path);
两者都赋予相同的值。
以上是 如何在Java中定义相对路径 的全部内容, 来源链接: utcz.com/qa/433178.html








