파일이라는 클래스 java.io 패키지의 는 시스템의 파일 또는 디렉토리(경로 이름)를 나타냅니다. 이 클래스는 파일/디렉토리에 대한 다양한 작업을 수행하는 다양한 메서드를 제공합니다.
mkdir() 이 클래스의 메서드는 현재 개체가 나타내는 경로로 디렉터리를 만듭니다.
디렉토리 계층 구조 만들기
mkdirs() 메소드를 사용하여 새 디렉토리의 계층 구조를 만들 수 있습니다. 같은 클래스의. 이 메서드는 존재하지 않는 상위 디렉터리를 포함하여 현재 개체가 나타내는 경로로 디렉터리를 만듭니다.
예시
import java.io.File;
import java.util.Scanner;
public class CreateDirectory {
public static void main(String args[]) {
System.out.println("Enter the path to create a directory: ");
Scanner sc = new Scanner(System.in);
String path = sc.next();
System.out.println("Enter the name of the desired a directory: ");
path = path+sc.next();
//Creating a File object
File file = new File(path);
//Creating the directory
boolean bool = file.mkdirs();
if(bool) {
System.out.println("Directory created successfully");
}else {
System.out.println("Sorry couldnt create specified directory");
}
}
} 출력
Enter the path to create a directory: D:\test\myDirectories\ Enter the name of the desired a directory: sample_directory Directory created successfully
확인하면 생성된 디렉토리를 −
로 볼 수 있습니다.
