これは一見単純な問題ですが、私はそれをきれいに行うのに苦労しています。私は次のようなファイルパスを持っています:
/ this / is / an / abstract / 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で提供されたパスからサブパスを抽出するより良い方法を提案できますか?
ありがとう