[Java] 폴더 및 파일 생성
// 폴더 경로
String path = "C:\\TEST\\TEST1234\\ABC";
// 객체 생성
File folder = new File(path);
// 해당 경로에 폴더가 없으면 생성
if ( !folder.exists() ) {
try {
System.out.println("폴더생성여부 : " + folder.mkdirs());
} catch (Exception e) {
e.getStackTrace();
}
}
// 파일 경로
path += "\\" + fileNm;
try {
// 파일 객체 생성
File file = new File(path);
// 파일 생성
file.createNewFile();
// 데이터 쓰고 닫기
FileOutputStream stream = new FileOutputStream(file);
stream.write(createData.getBytes());
stream.close();
} catch (Exception e) {
// TODO: handle exception
}
mkdir은 폴더 경로 중 없는 폴더가 있으면 생성되지 않는다.
mkdirs를 사용하면 최초에 아무것도 없을 경우에도
c -> TEST -> TEST1234 -> ABC 폴더와 최종 폴더 내 파일 모두 생성이 된다.
최근댓글