Here you can find the source of getRelativeName(File file, File root)
Parameter | Description |
---|---|
file | a parameter |
root | a parameter |
public static String getRelativeName(File file, File root)
//package com.java2s; //License from project: Apache License import java.io.File; public class Main { /**/*from w w w . j a v a2 s . com*/ * Strips off the part of the path before the start of the root file. If the root is c:\temp\foo * and the file is c:\temp\foo\folder\file.txt this will return folder\file.txt * * @param file * @param root * @return */ public static String getRelativeName(File file, File root) { int length = root.getAbsolutePath().length(); String absolutePath = file.getAbsolutePath(); String relativePath; if (absolutePath.length() == length) { if (file.getAbsolutePath().equals(root.getAbsolutePath())) { // They are the same file relativePath = ""; } else { throw new RuntimeException( String.format("Something looks wrong with the parameters : file [%s] and root [%s]", file.getAbsolutePath(), root.getAbsolutePath())); } } else { relativePath = absolutePath.substring(length + 1, absolutePath.length()); } return relativePath; } }