Java Relative Path Get relativeTo(File peer, String filename)

Here you can find the source of relativeTo(File peer, String filename)

Description

relative To

License

Open Source License

Declaration

public static File relativeTo(File peer, String filename) 

Method Source Code

//package com.java2s;
/*  Copyright (C) 2009 Mobile Sorcery AB
    //from   ww w .  ja va  2 s . c  o m
This program is free software; you can redistribute it and/or modify it
under the terms of the Eclipse Public License v1.0.
    
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 Eclipse Public License v1.0 for
more details.
    
You should have received a copy of the Eclipse Public License v1.0 along
with this program. It is also available at http://www.eclipse.org/legal/epl-v10.html
*/

import java.io.File;

public class Main {
    public static File relativeTo(File peer, String filename) {
        File filenameFile = new File(filename);
        if (filenameFile.isAbsolute()) {
            return filenameFile;
        }

        File dir = peer.isDirectory() ? peer : peer.getParentFile();
        return new File(dir, filename).getAbsoluteFile();
    }
}

Related

  1. relativePath(final File root, final File file)
  2. relativePath(final String inputPath, final File file)
  3. relativePath(String origin, String target)
  4. relativePath(String p_absolutePath, String p_currentPath)
  5. relativePathFiles(String relativePath, final String... fileNamesRegex)
  6. relativeToAbsoluteFilePath(String s)
  7. relativize(File base, File absolute)
  8. relativize(File base, File child)
  9. relativize(File file, File basedir)