Here you can find the source of createRelativePath(File baseDirFile, File file)
public static String createRelativePath(File baseDirFile, File file) throws IOException
//package com.java2s; /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ import java.io.File; import java.io.IOException; import java.nio.file.Path; public class Main { public static String createRelativePath(File baseDirFile, File file) throws IOException { if (baseDirFile == null) throw new IllegalArgumentException("baseDirFile must not be null"); if (file == null) throw new IllegalArgumentException("file must not be null"); Path basePath = baseDirFile.toPath().normalize(); Path filePath = file.toPath().normalize(); return basePath.relativize(filePath).toString(); }// w ww . j a va 2 s. co m public static String createRelativePath(String absoluteBaseDirPath, String absoluteFilePath) throws IOException { if (absoluteBaseDirPath == null) throw new IllegalArgumentException("baseDir must not be null"); if (absoluteFilePath == null) throw new IllegalArgumentException("file must not be null"); return createRelativePath(new File(absoluteBaseDirPath), new File(absoluteFilePath)); } }