flavio.donze flavio.donze - 4 months ago 9
Java Question

Best way to get a sub-path of a file

I have two

File
objects:


  1. C:/basepath/

  2. C:/basepath/directory/file.txt



Now I would like to subtract file 1 from file 2 so that I get
directory/file.txt
.

I don't want to use
String.substring()
since file paths may differ from input.

Answer

Use the features of java.nio.file.Path. You are looking to 'relativize'.