Here you can find the source of relativePath(File file, String path)
Parameter | Description |
---|---|
file | is the file to get a relative path from |
path | is the path |
public static String relativePath(File file, String path)
//package com.java2s; /*/*ww w.ja va2s . co m*/ * The MIT License (MIT) * * Copyright (c) 2015 Ziver Koc * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ import java.io.*; import java.util.regex.Matcher; public class Main { /** * Returns a String with a relative path from the given path * * @param file is the file to get a relative path from * @param path is the path * @return A String with a relative path */ public static String relativePath(File file, String path) { if (file == null || path == null) return null; String absolute = file.getAbsolutePath(); String tmpPath = path.replaceAll("[/\\\\]", Matcher.quoteReplacement(File.separator)); String relative = absolute.substring(absolute.indexOf(tmpPath) + path.length(), absolute.length()); return relative; } }