[java] 자바 파일 시스템에서 폴더 생성 및 삭제하기

폴더 생성

자바에서는 java.io.File 클래스를 사용하여 폴더를 생성할 수 있습니다. 아래는 폴더를 생성하는 간단한 예제 코드입니다.

import java.io.File;

public class CreateFolderExample {
    public static void main(String[] args) {
        File folder = new File("C:/exampleFolder");
        if (!folder.exists()) {
            folder.mkdirs();
            System.out.println("폴더가 생성되었습니다.");
        } else {
            System.out.println("해당 폴더는 이미 존재합니다.");
        }
    }
}

위 예제에서 C:/exampleFolder는 생성하려는 폴더의 경로를 나타냅니다. 폴더가 이미 존재하는지 확인한 후 mkdirs() 메서드를 사용하여 폴더를 생성합니다.

폴더 삭제

폴더 삭제 또한 java.io.File 클래스를 사용하여 수행할 수 있습니다. 아래는 폴더를 삭제하는 예제 코드입니다.

import java.io.File;

public class DeleteFolderExample {
    public static void main(String[] args) {
        File folder = new File("C:/exampleFolder");
        if (folder.exists()) {
            folder.delete();
            System.out.println("폴더가 삭제되었습니다.");
        } else {
            System.out.println("해당 폴더는 존재하지 않습니다.");
        }
    }
}

위 예제에서 C:/exampleFolder는 삭제하려는 폴더의 경로를 나타냅니다. 폴더의 존재 여부를 확인한 후 delete() 메서드를 사용하여 폴더를 삭제합니다.

폴더를 생성하고 삭제하는 방법에 대해 간단히 알아보았습니다. 자바에서는 java.io.File 클래스를 사용하여 파일 및 폴더 관리 작업을 수행할 수 있습니다.