@Test public void test_getSameSubfixPath(){ String path1 = "D:\\0000_test\\updiff\\abacus\\abacus-product-bootstrap-1.0.0_backup_20150817141952\\lib\\abacus-business-domain-transjnl-1.0.0.jar_new.jar"; String path2 = "D:\\a\\b\\d\\1\\2\\3\\4.java"; String samePath = FilePathUtils.getSameSubfixPath(path1, path2); System.out.println(samePath); assert samePath.equals("1/2/3/4.java"); }
public static String getSameSubfixPath(String path1, String path2){ if(path1.equals(path2)) return path1; path1 = path1.replace("\\", "/"); path2 = path2.replace("\\", "/"); Stack<String> path1Stack = new Stack<String>(); for(String path: path1.split("/")){ path1Stack.push(path); } Stack<String> path2Stack = new Stack<String>(); for(String path: path2.split("/")){ path2Stack.push(path); } StringBuilder sb = new StringBuilder(); while(!path1Stack.empty()){ String p1 = path1Stack.pop(); String p2 = path2Stack.pop(); if(!p1.equals(p2)){ if(sb.length() > 0){ return sb.toString().substring(1); } else { return null; } } else { sb.insert(0, "/" + p1); } } return null; }
FilePathUtils { public static String getSameSubfixPath(String path1, String path2){ if(path1.equals(path2)) return path1; path1 = path1.replace("\\", "/"); path2 = path2.replace("\\", "/"); Stack<String> path1Stack = new Stack<String>(); for(String path: path1.split("/")){ path1Stack.push(path); } Stack<String> path2Stack = new Stack<String>(); for(String path: path2.split("/")){ path2Stack.push(path); } StringBuilder sb = new StringBuilder(); while(!path1Stack.empty()){ String p1 = path1Stack.pop(); String p2 = path2Stack.pop(); if(!p1.equals(p2)){ if(sb.length() > 0){ return sb.toString().substring(1); } else { return null; } } else { sb.insert(0, "/" + p1); } } return null; } }
FilePathUtils { public static String getSameSubfixPath(String path1, String path2){ if(path1.equals(path2)) return path1; path1 = path1.replace("\\", "/"); path2 = path2.replace("\\", "/"); Stack<String> path1Stack = new Stack<String>(); for(String path: path1.split("/")){ path1Stack.push(path); } Stack<String> path2Stack = new Stack<String>(); for(String path: path2.split("/")){ path2Stack.push(path); } StringBuilder sb = new StringBuilder(); while(!path1Stack.empty()){ String p1 = path1Stack.pop(); String p2 = path2Stack.pop(); if(!p1.equals(p2)){ if(sb.length() > 0){ return sb.toString().substring(1); } else { return null; } } else { sb.insert(0, "/" + p1); } } return null; }  }
FilePathUtils { public static String getSameSubfixPath(String path1, String path2){ if(path1.equals(path2)) return path1; path1 = path1.replace("\\", "/"); path2 = path2.replace("\\", "/"); Stack<String> path1Stack = new Stack<String>(); for(String path: path1.split("/")){ path1Stack.push(path); } Stack<String> path2Stack = new Stack<String>(); for(String path: path2.split("/")){ path2Stack.push(path); } StringBuilder sb = new StringBuilder(); while(!path1Stack.empty()){ String p1 = path1Stack.pop(); String p2 = path2Stack.pop(); if(!p1.equals(p2)){ if(sb.length() > 0){ return sb.toString().substring(1); } else { return null; } } else { sb.insert(0, "/" + p1); } } return null; }  static String getWorkingPath(); static String getAbstractePath(String relativePath); static String getSameSubfixPath(String path1, String path2); }
FilePathUtils { public static String getSameSubfixPath(String path1, String path2){ if(path1.equals(path2)) return path1; path1 = path1.replace("\\", "/"); path2 = path2.replace("\\", "/"); Stack<String> path1Stack = new Stack<String>(); for(String path: path1.split("/")){ path1Stack.push(path); } Stack<String> path2Stack = new Stack<String>(); for(String path: path2.split("/")){ path2Stack.push(path); } StringBuilder sb = new StringBuilder(); while(!path1Stack.empty()){ String p1 = path1Stack.pop(); String p2 = path2Stack.pop(); if(!p1.equals(p2)){ if(sb.length() > 0){ return sb.toString().substring(1); } else { return null; } } else { sb.insert(0, "/" + p1); } } return null; }  static String getWorkingPath(); static String getAbstractePath(String relativePath); static String getSameSubfixPath(String path1, String path2);  }