코딩 관련/Java
[Java] 현재 경로 / 디렉터리 경로 / 현재 디렉터리 / 디렉터리 내용 출력
메리짱123
2021. 11. 9. 21:11
반응형
1. System.getProperty("user.dir")
현재 작업 path
String cwd = System.getProperty("user.dir");
System.out.println("현재 경로 : "+cwd);
//현재 실행 프로젝트 경로가 출력됨
//현재 경로 : D:\myProject
2. listFiles()
특정 파일/디렉터리 아래의 파일들 full path
File dirs = new File(cwd + "\\src\\main\\resources\\static\\assets\\data");
File dir[] = dirs.listFiles();
for(int i=0;i<dir.length;i++){
System.out.println("지정한 경로 아래의 파일 경로 : " + dir[i]);
}
//디렉터리 아래에 있는 파일or디렉터리들의 full path가 출력됨.
//지정한 경로 아래의 파일 경로 : D:\myProject\src\main\resources\static\assets\data\file1
//지정한 경로 아래의 파일 경로 : D:\myProject\src\main\resources\static\assets\data\fil2
//지정한 경로 아래의 파일 경로 : D:\myProject\src\main\resources\static\assets\data\folder1
3. list()
특정 파일/디렉터리 아래의 파일들 이름 출력
File dirs = new File(cwd + "\\src\\main\\resources\\static\\assets\\data");
String dirName[] = dirs.list();
for(int i=0;i<dirName.length;i++){
System.out.println("지정한 경로 아래의 파일 : " + dirName[i]);
}
//디렉터리 아래에 있는 파일or디렉터리들의 이름만 출려됨
//지정한 경로 아래의 파일 : file1
//지정한 경로 아래의 파일 : file2
//지정한 경로 아래의 파일 : folder1
반응형