Here you can find the source of getRelativeURL(File base, File file)
Parameter | Description |
---|---|
IOException | if position of file is not relative to base |
public static String getRelativeURL(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. * same as {@link #getRelativePath(File, File)} but replaces windows-plattform-specific * file separator <code>\</code> with <code>/</code> * @throws IOException if position of file is not relative to base *///from w ww. j a v a2 s . c om public static String getRelativeURL(File base, File file) throws IOException { StringBuffer result = new StringBuffer(getRelativePath(base, file)); for (int i = 0; i < result.length(); i++) { if (result.charAt(i) == '\\') result.setCharAt(i, '/'); } return result.toString(); } /** returns the relative path of file to base. * @throws IOException if position of file is not relative to base */ 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()); } }