質問

I just encountered behavior I do not understand:

Path path = Paths.get("somefile.txt");
System.out.println(path.getAbsolutePath());
System.out.println(path.getParent());

Well, to be honest, I was debugging some code and I evaluated this code on runtime and path was resolved as WindowsPath instance, therefore there was method getAbsolutePath available.

When I ran the code, parent was null but absolutePath printed the absolute path. So If the object knows absolute path and there is parent directory why does it return null?

役に立ちましたか?

解決

you have to give the complete path /home/username/filename.txt instead somefile.txt

他のヒント

According to the javadoc, the Path#getParent() method :

Returns the parent path, or null if this path does not have a parent.

and

This method does not access the file system; the path or its parent may not exist

This means that "somefile.txt" doesn't have a parent path in the context of the root of your application.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top