Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path.getParent() is null

Tags:

java

nio

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?

like image 253
Leos Literak Avatar asked Dec 16 '22 00:12

Leos Literak


1 Answers

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

like image 118
Shriram Avatar answered Dec 26 '22 19:12

Shriram