[JAVA] JAVA 파일경로 관련 정리
2021. 1. 3. 00:10ㆍ백엔드/JAVA
728x90
new File().getAbsolutePath(); [ 절대경로 ]
new File().getCanonicalPath(); [ 상대경로 ]
* './' :현재디렉터리
* '../' :상위디렉터리
new File("../conf.properties").getAbsolutePath(); /* C:\Users\user\eclipse-workspace\Test\..\conf.properties */
new File("../conf.properties").getCanonicalPath(); /* C:\Users\user\eclipse-workspace\conf.properties */
new File("./conf.properties").getAbsolutePath(); /* C:\Users\user\eclipse-workspace\Test\.\conf.properties */
new File("./conf.properties").getCanonicalPath(); /* C:\Users\user\eclipse-workspace\Test\conf.properties */
new File("conf.properties").getAbsolutePath(); /* C:\Users\user\eclipse-workspace\Test\conf.properties */
new File("conf.properties").getCanonicalPath(); /* C:\Users\user\eclipse-workspace\Test\conf.properties */
ClassName.class.getResource("").getPath();
ClassName.class.getProtectionDomain().getCodeSource().getLocation();
String dir1_1 = FilePath.class.getResource("").getPath(); /* /C:/Users/user/eclipse-workspace/Test/bin/ */
String dir1_2 = FilePath.class.getResource("/").getPath(); /* /C:/Users/user/eclipse-workspace/Test/bin/ */
String dir1_3 = FilePath.class.getResource("conf.properties").getPath(); /* /C:/Users/user/eclipse-workspace/Test/bin/conf.properties */
String dir2_1 = FilePath.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath(); /* /C:/Users/user/eclipse-workspace/Test/bin/ */
String dir2_2 = FilePath.class.getProtectionDomain().getCodeSource().getLocation().getPath(); /* /C:/Users/user/eclipse-workspace/Test/bin/ */
URI dir2_3 = FilePath.class.getProtectionDomain().getCodeSource().getLocation().toURI(); /* file:/C:/Users/user/eclipse-workspace/Test/bin/ */
URL.toURI();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
String dir = FilePath.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
URL url = classLoader.getResource("conf.properties");
url.toString(); /* file:/C:/Users/user/eclipse-workspace/Test/bin/conf.properties */
url.toURI(); /* file:/C:/Users/user/eclipse-workspace/Test/bin/conf.properties */
System.getProperty("user.home"); [사용자 홈 디렉토리]
System.getProperty("user.dir"); [상위 폴더 디렉토리]
System.getProperty("user.home") + System.getProperty("file.separator"); /* [사용자홈디렉토리] C:\Users\user\eclipse-workspace\Test\ */
System.getProperty("user.dir") + System.getProperty("file.separator"); /*[프로퍼티의 상위 폴더경로] C:\Users\user\eclipse-workspace\Test\ */프로퍼티의 상위 폴더경로
728x90
'백엔드 > JAVA' 카테고리의 다른 글
[설치] JAVA 1.8 설치방법 (0) | 2021.02.06 |
---|---|
[JAVA] 자료구조 - ArrayList, LinkedList, Stack, Queue (0) | 2021.01.14 |
[JAVA] Java Resource 사용(getResource(),getResourceAsStream()) (0) | 2020.12.28 |
[JAVA] JAVA 프로그램 실행구조 / JAVA 환경변수설정 (0) | 2020.12.28 |
[JAVA] cmd에서 자바 컴파일 후 실행 방법 (0) | 2020.12.28 |