计算文件之间的相对路径
// Copyright (C) 2001-2012 Michael Bayne, et al. //package com.nowjava; import java.io.File; /** from NowJava.com - 时代Java**/ import java.io.IOException; public class Main { /** * Computes a relative path between to Files * * @param file the file we're referencing * @param relativeTo the path from which we want to refer to the file */ public static String computeRelativePath(File file, File relativeTo) throws IOException { String[] realDirs = getCanonicalPathElements(file); String[] relativeToDirs = getCanonicalPathElements(relativeTo); // Eliminate the common root int common = 0; for (; common < realDirs.length && common < relativeToDirs.length; common++) { if (!realDirs[common].equals(relativeToDirs[common])) { break; } } String relativePath = ""; /** 来 自 nowjava.com - 时 代 Java **/ // For each remaining level in the file path, add a .. for (int ii = 0; ii < (realDirs.length - common); ii++) { relativePath += ".." + File.separator; } // For each level in the resource path, add the path for (; common < relativeToDirs.length; common++) { relativePath += relativeToDirs[common] + File.separator; } return relativePath; } /** * Gets the individual path elements building up the canonical path to the given file. */ public static String[] getCanonicalPa