'개발이야기 > C, C++' 카테고리의 다른 글
포인터 배열 테스트 코드 (0) | 2012.08.07 |
---|---|
C코드 메모리릭 잡기 (0) | 2012.07.18 |
부모 클래스의 기본생성자가 없을때 메모리 누수 현상 (2) | 2012.06.12 |
마방진 원리 및 문제 (4) | 2012.05.19 |
Mangled Name (0) | 2012.05.13 |
포인터 배열 테스트 코드 (0) | 2012.08.07 |
---|---|
C코드 메모리릭 잡기 (0) | 2012.07.18 |
부모 클래스의 기본생성자가 없을때 메모리 누수 현상 (2) | 2012.06.12 |
마방진 원리 및 문제 (4) | 2012.05.19 |
Mangled Name (0) | 2012.05.13 |
두 절대경로에서 상대경로를 구하는 JAVA 코드입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | /** * fromPath 절대경로에서 toPath 절대경로에 대한 상대경로를 반환 * * @param fromPath 절대경로 * @param toPath 절대경로 * @return 상대경로 */ public static String makeRelativePath(String fromPath, String toPath) { if ( fromPath == null || fromPath.isEmpty() ) return null ; if ( toPath == null || toPath.isEmpty() ) return null ; StringBuilder relativePath = null ; fromPath = fromPath.replaceAll( "\\\\" , "/" ); toPath = toPath.replaceAll( "\\\\" , "/" ); if (fromPath.equals(toPath) == true ) { } else { String[] absoluteDirectories = fromPath.split( "/" ); String[] relativeDirectories = toPath.split( "/" ); //Get the shortest of the two paths int length = absoluteDirectories.length < relativeDirectories.length ? absoluteDirectories.length : relativeDirectories.length; //Use to determine where in the loop we exited int lastCommonRoot = - 1 ; int index; //Find common root for (index = 0 ; index < length; index++) { if (absoluteDirectories[index].equals(relativeDirectories[index])) { lastCommonRoot = index; } else { break ; //If we didn't find a common prefix then throw } } if (lastCommonRoot != - 1 ) { //Build up the relative path relativePath = new StringBuilder(); //Add on the .. for (index = lastCommonRoot + 1 ; index < absoluteDirectories.length; index++) { if (absoluteDirectories[index].length() > 0 ) { relativePath.append( "../" ); } } for (index = lastCommonRoot + 1 ; index < relativeDirectories.length - 1 ; index++) { relativePath.append(relativeDirectories[index] + "/" ); } relativePath.append(relativeDirectories[relativeDirectories.length - 1 ]); } } return relativePath == null ? null : relativePath.toString(); } |
references
1인치의 혁신 (0) | 2014.05.07 |
---|---|
안철수의 생각 (0) | 2012.09.03 |
오리진이 되라 (0) | 2012.05.12 |
글로벌 소프트웨어를 꿈꾸다 (0) | 2012.05.11 |