[英]Java.nio.file.Paths is giving incorrect path for current directory?
I am trying to read the contents of a file using Java.nio.file.Paths class, my code looks like this 我正在尝试使用Java.nio.file.Paths类读取文件的内容,我的代码如下所示
package com.test.json;
Path currentDir = Paths.get(".");
System.out.println(currentDir.toAbsolutePath());
It is giving me the path 它给我道路
/home/rohit/workspace/MapReduceExample/.
while the output should be 而输出应该是
home/rohit/workspace/MapReduceExample/src/com/test/json/
It is ignoring the component of src folder and packages. 它忽略了src文件夹和软件包的组件。 Could someone please tell me what I am doing wrong?
有人可以告诉我我做错了吗?
I can't give absolute path because I need this code for a map-reduce path, I have to construct path in relative manner. 我不能给出绝对路径,因为我需要此代码来实现map-reduce路径,我必须以相对方式构造路径。 So, my approach is to do
所以,我的方法是
getCurrentDirectoryPath + filename
It appears to me that you are expecting the Path
of the directory where your source Java file (that calls Paths.get(".")
) resides. 在我看来,您期望源Java文件(调用
Paths.get(".")
)所在的目录的Path
。 But that's not what the path "."
但这不是路径
"."
will fetch. 将获取。 When a JVM runs your class on the host file system, the value of "."
当JVM在主机文件系统上运行您的类时,值“。”。 refers to the current working directory of the JVM process.
指JVM进程的当前工作目录。 It's very likely that the JVM that runs your class is actually started in that folder:
/home/rohit/workspace/MapReduceExample
. 运行您的类的JVM很可能实际上是在该文件夹中启动的:
/home/rohit/workspace/MapReduceExample
。 If you did a 如果你做了
System.out.println(Paths.get(
System.getProperty("user.dir")).toAbsolutePath());
you'll see that it prints the same folder, without the trailing "."
您会看到它打印相同的文件夹,没有尾随
"."
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.