Source Code : Get relative Path

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Get relative Path

        
//package xcordion.util;

import java.io.File;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class FileUtils {

  static public String relativePath(File from, File to) {
    return relativePath(from, to, File.separatorChar);
  }

  static public String relativePath(File from, File to, char separatorChar) {
    String fromPath = from.getAbsolutePath();
    String toPath = to.getAbsolutePath();
    boolean isDirectory = from.isDirectory();
    return relativePath(fromPath, toPath, isDirectory, separatorChar);
  }

  public static String relativePath(String fromPath, String toPath,
      boolean fromIsDirectory, char separatorChar) {
    ArrayList<String> fromElements = splitPath(fromPath);
    ArrayList<String> toElements = splitPath(toPath);
    while (!fromElements.isEmpty() && !toElements.isEmpty()) {
      if (!(fromElements.get(0).equals(toElements.get(0)))) {
        break;
      }
      fromElements.remove(0);
      toElements.remove(0);
    }

    StringBuffer result = new StringBuffer();
    for (int i = 0; i < fromElements.size() - (fromIsDirectory ? 0 : 1); i++) {
      result.append("..");
      result.append(separatorChar);
    }
    for (String s : toElements) {
      result.append(s);
      result.append(separatorChar);
    }
    return result.substring(0, result.length() - 1);
  }

  private static ArrayList<String> splitPath(String path) {
    ArrayList<String> pathElements = new ArrayList<String>();
    for (StringTokenizer st = new StringTokenizer(path, File.separator); st
        .hasMoreTokens();) {
      String token = st.nextToken();
      if (token.equals(".")) {
        // do nothing
      } else if (token.equals("..")) {
        if (!pathElements.isEmpty()) {
          pathElements.remove(pathElements.size() - 1);
        }
      } else {
        pathElements.add(token);
      }
    }
    return pathElements;
  }
}

   
    
    
    
    
    
    
    
  

Thank with us