Whi*_*g34 9

Java中没有任何方法可以执行您想要的操作.也许在那里有一个库可以做到这一点(我想不出任何副手,而且Apache Commons IO似乎没有它).你可以使用这个或类似的东西:

// returns null if file isn't relative to folder
public static String getRelativePath(File file, File folder) {
    String filePath = file.getAbsolutePath();
    String folderPath = folder.getAbsolutePath();
    if (filePath.startsWith(folderPath)) {
        return filePath.substring(folderPath.length() + 1);
    } else {
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)