문제

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