Here you can find the source of getRelativePath(File base, File file)
Parameter | Description |
---|---|
IOException | if position of file is not relative to base |
public static String getRelativePath(File base, File file) throws IOException
//package com.java2s; /*--------------------------------------------------------------------------* | Copyright (C) 2006 Christopher Kohlhaas | | | | This program is free software; you can redistribute it and/or modify | | it under the terms of the GNU General Public License as published by the | | Free Software Foundation. A copy of the license has been included with | | these distribution in the COPYING file, if not go to www.fsf.org | | | | As a special exception, you are granted the permissions to link this | | program with every library, which license fulfills the Open Source | | Definition as published by the Open Source Initiative (OSI). | *--------------------------------------------------------------------------*/ import java.io.File; import java.io.IOException; public class Main { /** returns the relative path of file to base. * @throws IOException if position of file is not relative to base *///from w ww.j a va 2s. c om public static String getRelativePath(File base, File file) throws IOException { String filePath = file.getAbsoluteFile().getCanonicalPath(); String basePath = base.getAbsoluteFile().getCanonicalPath(); int start = filePath.indexOf(basePath); if (start != 0) throw new IOException(basePath + " not ancestor of " + filePath); return filePath.substring(basePath.length()); } }