[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