简体   繁体   English

Java.nio.file.Paths为当前目录提供了错误的路径?

[英]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.

相关问题 如何在 nashorn javascript 中使用 Paths(java.nio.file.Paths) - How to use Paths(java.nio.file.Paths) in nashorn javascript Java6中java.nio.file.Paths的替代方法 - Alternative to java.nio.file.Paths in Java6 java.nio.file.Paths - 如何在迭代时跳过无法访问的文件/文件夹 - java.nio.file.Paths - How to skip unaccessible file/folder while iterating JMeter - 在类'java.nio.file.Paths'中找不到静态方法get(java.lang.String) - JMeter - Static method get( java.lang.String ) not found in class'java.nio.file.Paths' 使用java.nio.file.Paths接口时缺少方案(IllegalArgumentException) - Missing scheme (IllegalArgumentException) while using java.nio.file.Paths interface 使用 java.nio.file.Paths & jsfml loadFromFile 时发生死锁 - Deadlock occurring when using java.nio.file.Paths & jsfml loadFromFile 复制文件时,JMeter Bean Shell采样器错误“在类'java.nio.file.Paths中找不到静态方法get(java.lang.String)” - JMeter Bean Shell Sampler error “…Static method get( java.lang.String ) not found in class'java.nio.file.Paths” when copying files Java中IO和NIO的路径 - Path and paths of IO and NIO in java Java:如何使用nio Path规范化路径? - Java: how to normalize paths with nio Path? Java NIO搜索目录中的文件 - Java NIO Search file in a directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM