Here you can find the source of getRelativePath(File fromFolder, File toFile)
Parameter | Description |
---|---|
fromFolder | The parent directory |
toFile | The file |
Parameter | Description |
---|---|
IOException | On error, for instance if toFile is not in fromFolder |
public static String getRelativePath(File fromFolder, File toFile) throws IOException
//package com.java2s; /**/*from w w w. j a v a2 s . c om*/ * Copyright (C) 2000-2010 Atomikos <info@atomikos.com> * * This code ("Atomikos TransactionsEssentials"), by itself, * is being distributed under the * Apache License, Version 2.0 ("License"), a copy of which may be found at * http://www.atomikos.com/licenses/apache-license-2.0.txt . * You may not use this file except in compliance with the License. * * While the License grants certain patent license rights, * those patent license rights only extend to the use of * Atomikos TransactionsEssentials by itself. * * This code (Atomikos TransactionsEssentials) contains certain interfaces * in package (namespace) com.atomikos.icatch * (including com.atomikos.icatch.Participant) which, if implemented, may * infringe one or more patents held by Atomikos. * It should be appreciated that you may NOT implement such interfaces; * licensing to implement these interfaces must be obtained separately from Atomikos. * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ import java.io.File; import java.io.IOException; public class Main { /** * Gets the relative path of a file wrt one of its parent directories. * * @param fromFolder The parent directory * @param toFile The file * @return String The relative path * @throws IOException On error, for instance if toFile is not in fromFolder */ public static String getRelativePath(File fromFolder, File toFile) throws IOException { String basePath = fromFolder.getAbsolutePath(); String filePath = toFile.getAbsolutePath(); if (!filePath.startsWith(basePath)) throw new IOException("The specified file is not within the folder."); return filePath.substring(basePath.length() + 1); } }