【问题标题】:How to go back one directory from working directory in Java?如何从Java中的工作目录返回一个目录?
【发布时间】:2015-07-01 19:08:52
【问题描述】:

我正在使用此代码来读取 Maven 模块中的文件内容。该文件存储在工作目录之前的一个目录中。

String configFilePath = System.getProperty("user.dir") + "../conf/gce-configuration.xml";

File configFile = new File(configFilePath);

然后,当我尝试读取文件时,它给了我文件未找到错误。

找不到文件: /home/xxxx/bin../conf/gce-configuration.xml

我尝试了不同的方法,但仍然出现此错误。我在这里做错了什么?

编辑:好像我忘了在 maven 模块的 assembly/bin.xml 中提及文件名。所以xml文件不包括打包。我的错!

【问题讨论】:

    标签: java maven filenotfoundexception


    【解决方案1】:

    当你想比当前目录高一级时,你可以这样做

     String configFilePath = new File(System.getProperty("user.dir")).getParent() + "/conf/gce-configuration.xml";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-28
      • 1970-01-01
      • 2012-11-21
      • 1970-01-01
      • 2023-03-14
      • 2013-11-09
      • 1970-01-01
      相关资源
      最近更新 更多