How to get relative file path to folder using java

Possible duplicate:
How to build a relative path in Java from two absolute paths (or URLs)?

using java, is there a way to return the relative path of a file to a given folder?

+4
source share
2 answers

In Java, there is no way to include what you want. Maybe there is a library out there somewhere (I can't think of anything out of myself, and Apache Commons IO doesn't seem to have this). You can use this or something like this:

// 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; } } 
+9
source

Source: https://habr.com/ru/post/1344535/


All Articles