728x90
반응형
디렉토리내 파일이 존재 하는지 확인하는 java 코드입니다 .
디렉토리를 조회후 파일이 있는지 확인하고 디렉토리내 또 다른 디렉토리가 존재 한다면 재귀 함수 호출을 통해
하위 파일 까지 출력 하는 코드 입니다.
Oracle 샘플 스키마 스크립트를 가지고 테스트해보겠습니다.
import java.io.File;
import java.io.IOException;
import java.security.AccessControlException;
import java.sql.SQLException;
import java.util.ArrayList;
public class DirectoryFileList {
public static void main(String[] args)
throws AccessControlException, SQLException, ClassNotFoundException, IOException {
String path = "D:\\998.Tmp\\db-sample-schemas-19.2\\db-sample-schemas-19.2";
ArrayList al = new ArrayList<String>();
subDirList(path, al);
}
public static void subDirList(String source, ArrayList<String> list) {
File dir = new File(source);
File[] fileList = dir.listFiles();
try {
for (int i = 0; i < fileList.length; i++) {
File file = fileList[i];
if (file.isFile()) {
// 파일이 있다면 파일 이름 출력
System.out.println("\t File name :" + file.getAbsoluteFile());
list.add(file.getParent() + File.separator + file.getName());
} else if (file.isDirectory()) {
System.out.println("Directory name:" + file.getName());
// 서브디렉토리가 존재하면 재귀적 방법으로 다시 탐색
list.add(file.getParent() + File.separator + file.getName());
subDirList(file.getCanonicalPath().toString(), list);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
실행결과
Directory name:bus_intelligence
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\bus_intelligence\bi_main.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\bus_intelligence\bi_oe_pr.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\bus_intelligence\bi_sh_pr.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\bus_intelligence\bi_views.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\CONTRIBUTING.md
Directory name:customer_orders
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\co_ddl.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\co_dml.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\co_drop_objects.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\co_drop_user.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\co_main.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\co_set_identity_starts.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\co_user.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\customers.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\orders.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\order_items.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\products.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\README.md
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\README.txt
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\sample_queries.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\stores.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\drop_sch.sql
Directory name:human_resources
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\human_resources\hr_analz.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\human_resources\hr_code.sql
.
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\bus_intelligence\bi_main.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\bus_intelligence\bi_oe_pr.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\bus_intelligence\bi_sh_pr.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\bus_intelligence\bi_views.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\CONTRIBUTING.md
Directory name:customer_orders
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\co_ddl.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\co_dml.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\co_drop_objects.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\co_drop_user.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\co_main.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\co_set_identity_starts.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\co_user.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\customers.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\orders.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\order_items.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\products.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\README.md
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\README.txt
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\sample_queries.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\customer_orders\stores.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\drop_sch.sql
Directory name:human_resources
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\human_resources\hr_analz.sql
File name :D:\998.Tmp\db-sample-schemas-19.2\db-sample-schemas-19.2\human_resources\hr_code.sql
.
728x90
반응형
'03.Program > 02.java' 카테고리의 다른 글
[Java] JDBC 프로그래밍 (0) | 2022.04.20 |
---|---|
[Java basic-Utility] 파일 비교 (0) | 2022.03.27 |
Linux OpenJdk 설치 (0) | 2022.03.21 |