Android Path Relative Get getParentRelativeTo(File fileChild, File fileRoot)

Here you can find the source of getParentRelativeTo(File fileChild, File fileRoot)

Description

Returns a String that represents that part of fileChild's parent directory path which is contained inside fileRoot (that is, fileChild's parent's path relative to fileRoot).

License

Open Source License

Exception

Parameter Description
IllegalArgumentException if file == null; if root == null; file has no parent; if root does not contain file's parent

Declaration

public static String getParentRelativeTo(File fileChild, File fileRoot)
        throws IllegalArgumentException 

Method Source Code

/*/*from   w  w  w. j a v a  2  s  .  c om*/
Copyright ? 2008 Brent Boyer

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public License for more details.

You should have received a copy of the Lesser GNU General Public License along with this program (see the license directory in this project).  If not, see <http://www.gnu.org/licenses/>.
 */

import bb.science.FormatUtil;
import bb.util.Check;
import bb.util.StringUtil;
import bb.util.ThrowableUtil;
import bb.util.logging.LogUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.util.Random;
import java.util.logging.Level;
import org.junit.Assert;
import org.junit.Test;

public class Main{
    /**
     * Returns a String that represents that part of fileChild's parent directory path which is contained inside fileRoot
     * (that is, fileChild's parent's path relative to fileRoot).
     * For example, if fileChild.getPath() = "/dirA/dirB/dirC/file1" and fileRoot.getPath() = "/dirA/dirB"
     * then the result is "dirC".
     * Note that the result never begins with a path separator char.
     * Neither File need actually exist; this method simply does String operations on their paths.
     * <p>
     * @throws IllegalArgumentException if file == null; if root == null; file has no parent; if root does not contain file's parent
     */
    public static String getParentRelativeTo(File fileChild, File fileRoot)
            throws IllegalArgumentException {
        Check.arg().notNull(fileChild);
        Check.arg().notNull(fileRoot);

        File parent = fileChild.getParentFile();
        Check.arg().notNull(parent);

        String pathParent = parent.getPath();
        String pathRoot = fileRoot.getPath();
        if (!pathParent.startsWith(pathRoot))
            throw new IllegalArgumentException("fileChild's parent = "
                    + pathParent + " is not contained inside fileRoot = "
                    + pathRoot);

        return pathParent.substring(pathRoot.length() + 1); // the + 1 skips over the directory separator char
    }
}

Related

  1. getPathRelativeTo(File fileChild, File fileRoot)
  2. getRelativeFileName(File file, File basedir)
  3. getRelativePath(File parent, File file)
  4. getWindowsRelativePath(String basedir, String path)
  5. relativePathFromBase(File file, File basedir)