java nio – `Paths.get()`如何在项目根目录中查找文件夹?

huangapple go评论74阅读模式
英文:

java nio - How does Paths.get() finds a folder in the root directory of a project?

问题

我在 Eclipse 项目(TestProject)中运行了下面提到的代码。我在项目根目录下创建了这个文件夹(名为 FilesToBeProcessed)。

private static Path pathDirectory = java.nio.file.Paths.get("FilesToBeProcessed");

pathDirectory 解析为完整路径 = "C:\eclipse\workspace-poc\TestProject\FilesToBeProcessed"

它是如何解析出这个完整路径的?因为我的类是从一个包内执行的,所以它并没有在当前目录中查找文件夹(FilesToBeProcessed),实际上它是如何知道要在 "C:\eclipse\workspace-poc\TestProject" 目录下查找的呢?

英文:

I am running below mentioned code in an eclipse project(TestProject). I created this folder(named FilesToBeProcessed) inside project root.

private static Path pathDirectory = java.nio.file.Paths.get("FilesToBeProcessed");

pathDirectory gets resolved to full path = "C:\eclipse\workspace-poc\TestProject\FilesToBeProcessed"

How does it resolves this full path, my class is getting executed from inside a package so its not looking into current directory for folder(FilesToBeProcessed), basically how does it know to look inside "C:\eclipse\workspace-poc\TestProject" ?

答案1

得分: 3

相对路径通常相对于当前工作目录解析,该目录是进程执行环境的一部分。它的设置取决于启动进程的方式:

  • 当你从集成开发环境(IDE)启动应用程序时,当前工作目录根据IDE中的项目启动配置进行设置。如果你没有进行更改,默认情况下是项目目录。具体来说,对于Eclipse,请参阅https://stackoverflow.com/questions/21028690/how-do-i-change-the-working-directory-for-an-eclipse-run-configuration

  • 当你从命令行启动应用程序时,它被设置为你所在的shell的当前目录。

  • 当你从图形菜单启动GUI应用程序或者通过双击图标启动时,当前工作目录取决于系统,但通常是用户的主目录或其中的子目录。

英文:

Relative paths are generally resolved against the current working directory, which is part of the execution environment of a process. How it's set depends on how you start the process:

  • When you start an application from an IDE, the current working directory is set according to the project launch configuration in the IDE. If you haven't changed it, the default is the project directory. For Eclipse concretely, see https://stackoverflow.com/questions/21028690/how-do-i-change-the-working-directory-for-an-eclipse-run-configuration

  • When you start an application from the command line, it's set to the current directory of the shell you are in.

  • When you start a GUI application from a graphical menu or by double-clicking on an icon, the current working directory depends on the system but it's typically the user's home directory, or a subdirectory therein

huangapple
  • 本文由 发表于 2020年8月20日 20:52:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63505537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定