1

I am wondering how to get the relative path name? Any existing library can help this?

c:\123 c:\123\1234\zaq\erf

I would like to get something like 123/1234/zaq/erf

user705414
  • 20,472
  • 39
  • 112
  • 155
  • you are saying that `c:\123 c:\123\1234\zaq\erf` will be passes as an argument and output is `123/1234/zaq/erf`? – havexz Dec 12 '11 at 02:42
  • 1
    Have a look at this question: http://stackoverflow.com/questions/204784/how-to-construct-a-relative-path-in-java-from-two-absolute-paths-or-urls – mikek3332002 Dec 12 '11 at 02:47

2 Answers2

4

Use the relativize() method in the URI class. Construct a URI from the base file path and then relativize your absolute file path.

String s = new File("c:\\123").toURI().relativize(new File("c:\\123\\1234\\zaq\\erf").toURI()).getPath();
tskuzzy
  • 35,812
  • 14
  • 73
  • 140
2

If you are using Java 7, there is the relativize method of the Path class that does what you need.

prunge
  • 22,460
  • 3
  • 73
  • 80