欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

写一个函数,算出两个文件的相对路径的

程序员文章站 2022-05-09 22:58:21
...
public class E{
public static void main(String[] args) throws Exception { 

     String pathA = "/a/b/c/d/g/m/1.txt";
     String pathB = "/c/b/c/d/g/h/2.txt";
     System.out.println(pathARelativePathBRecursion(pathA,pathB,""));
 } 

 /**
  * pathA相对于pathB的相对路径 递归算法:
  * 
  * @param pathA
  * @param pathB
  * @param i
  * @return
  */
 public static String pathARelativePathBRecursion(String pathA,String pathB, String tempPath) {
	System.out.println(tempPath);
	if (pathB==""||pathB==null)
	System.out.println("null");
	else 
	System.out.println(pathB);
	if (pathA.startsWith(pathB))
	return pathA.replaceFirst(pathB+"/",tempPath.substring(0,tempPath.length()-3));
	else
	return pathARelativePathBRecursion(pathA, pathB.substring(0, pathB.lastIndexOf("/")), "../" + tempPath);
 }

}

转载于:https://my.oschina.net/lvzjane/blog/92743