这是一个看似简单的问题,但我无法以干净的方式完成它。我有一个文件路径如下:
/this/is/an/absolute/path/to/the/location/of/my/file
我需要的是从上面给定的路径中提取 /of/my/file ,因为那是我的相对路径。
我想这样做的方式如下:
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();
这可能会满足我的直接需求,但是有人可以建议一种更好的方法来从 java 中提供的路径中提取子路径吗?
谢谢