質問

れがうまくいきません File 物のかを考えることができる以下の実施

public File convertToRelative(File home, File file) {
    final String homePath = home.getAbsolutePath();
    final String filePath = file.getAbsolutePath();

    // Only interested in converting file path that is a 
    // direct descendants of home path
    if (!filePath.beginsWith(homePath)) {
        return file;
    }

    return new File(filePath.substring(homePath.length()+1));
}

が賢く方法に変換するファイルの絶対パスは相対のファイルですか。

可能な重複:

どのように構築する相対パスにはjavaからの絶対パスまたはurl

役に立ちましたか?

解決

こうした前 こちらの

ここではその答えの場合

String path = "/var/data/stuff/xyz.dat";
String base = "/var/data";
String relative = new File(base).toURI().relativize(new File(path).toURI()).getPath();
// relative == "stuff/xyz.dat"
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top