3

I need to get the relative file path (relative to the program executable path) from a File object.

What's the best way to do this? File offers only methods for the absolute path. Maybe getting execution path manually and then cut this path off from the absolute path to get a relative path?

I am on Java 7, just in case java.nio has some helping method for that.

Tahnks for any hint!

stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270

2 Answers2

4

As you're using Java 7 you can make use of the new Path class, which has a number of really cool methods, including Path.relativize.

Tim Büthe
  • 62,884
  • 17
  • 92
  • 129
Sean Landsman
  • 7,033
  • 2
  • 29
  • 33
3

You need the method:

Path relativize(Path other)

of java.nio.file.Path.

To obtain a Path from a file you could use its Path toPath() method.

Carlo Pellegrini
  • 5,656
  • 40
  • 45