这似乎是一个简单的问题,但我很难用简洁的方式解决它。我有一个文件路径如下:
/this/is/an/absolute/path/to/the/location/of/my/file 我需要提取上述路径中的/of/my/file,因为这是我的相对路径。
我考虑的方法如下:
这可能能够满足我的即时需求,但有人能否建议一种更好的方法来从Java提供的路径中提取子路径?谢谢。
/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提供的路径中提取子路径?谢谢。