(자바) 디렉토리 생성

2022. 11. 16. 17:08자바

728x90

원하는 위치에 디렉토리가 없을경우 디렉토리 자동생성 

import java.io.File;
public class MkDir {
    public static void main(String[] args) {
		
	String path = "D:\\Eclipse\\Java\\새폴더"; //폴더 경로
	File Folder = new File(path);

	// 해당 디렉토리가 없을경우 디렉토리를 생성합니다.
	if (!Folder.exists()) {
		try{
		    Folder.mkdir(); //폴더 생성합니다.
		    System.out.println("폴더가 생성되었습니다.");
	        } 
	        catch(Exception e){
		    e.getStackTrace();
		}        
         }else {
		System.out.println("이미 폴더가 생성되어 있습니다.");
	}
    }
}
728x90