Path.getParent () - null

I just ran into behavior that I don't 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 at runtime, and the path was resolved as an instance of WindowsPath, so a method was available getAbsolutePath.

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

+4
source share
2 answers

you need to specify the full path /home/username/filename.txt, and not somefile.txt

+4
source

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

null, .

;

, "somefile.txt" .

+6

Source: https://habr.com/ru/post/1531721/


All Articles