This is a seemingly simple problem but I am having trouble doing it in a clean manner. I have a file path as follows:
/this/is/an/absolute/path/to/the/location/of/my/file
What I need is to extract /of/my/file from the above given path since that is my relative path.
The way I am thinking of doing it is as follows:
String absolutePath = "/this/is/an/absolute/path/to/the/location/of/my/file";
String[] tokenizedPaths = absolutePath.split("/");
int strLength = tokenizedPaths.length;
String myRelativePathStructure = (new StringBuffer()).append(tokenizedPaths[strLength-3]).append("/").append(tokenizedPaths[strLength-2]).append("/").append(tokenizedPaths[strLength-1]).toString();
This will probably serve my immediate needs but can somebody suggest a better way of extracting sub-paths from a provided path in java?
Thanks