프로그래밍 언어/JAVA
Java에서 getPath (), getAbsolutePath () 및 getCanonicalPath ()의 차이점
Rateye
2021. 6. 15. 10:46
728x90
반응형
질문 : Java에서 getPath (), getAbsolutePath () 및 getCanonicalPath ()의 차이점은 무엇입니까?
Java에서 getPath()
, getAbsolutePath()
및 getCanonicalPath()
의 차이점은 무엇입니까?
그리고 언제 각각을 사용합니까?
답변
다음 파일 이름을 고려하십시오.
C:\temp\file.txt
경로, 절대 경로 및 표준 경로입니다.
.\file.txt
경로입니다. 절대 경로도 표준 경로도 아닙니다.
C:\temp\myapp\bin\..\\..\file.txt
경로 및 절대 경로입니다. 표준 경로가 아닙니다.
표준 경로는 항상 절대 경로입니다.
경로에서 표준 경로로 변환하면 절대 경로가됩니다 (보통 현재 작업 디렉토리에 고정되므로 ./file.txt
가 c:/temp/file.txt
). 파일의 표준 경로는 경로를 "정화"하여 ..\
와 같은 항목을 제거 및 해결하고 (유닉스에서) 심볼릭 링크를 해결합니다.
또한 nio.Paths와 함께 다음 예제를 참고하십시오.
String canonical_path_string = "C:\\Windows\\System32\\";
String absolute_path_string = "C:\\Windows\\System32\\drivers\\..\\";
System.out.println(Paths.get(canonical_path_string).getParent());
System.out.println(Paths.get(absolute_path_string).getParent());
두 경로 모두 동일한 위치를 참조하지만 출력은 상당히 다릅니다.
C:\Windows C:\Windows\System32\drivers
출처 : https://stackoverflow.com/questions/1099300/whats-the-difference-between-getpath-getabsolutepath-and-getcanonicalpath
728x90
반응형