79337707

Date: 2025-01-08 00:41:01
Score: 0.5
Natty:
Report link

The previous answer gives a NullPointerException when File object has no path, namely, it is just a filename.

A minor modification works for that case as well

File file1 = ... // as before
File file2 = new File(file1.getCanonicalPath());
String path = file2.getParentFile().getCanonicalPath();
Reasons:
  • Low length (0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Nick