Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- 파이썬
- 수수료
- 특약
- javascript
- 가입
- 교보
- Vue.js
- 프론트엔드
- 리스트
- 심장질환
- 교보생명
- 문자열
- 인출수수료
- 뇌출혈
- 프로그래밍
- 중도인출
- Java
- 보험
- 웹개발
- PythonProgramming
- 자바스크립트
- 코딩
- python
- 사망
- 보험료
- 추가납입
- jQuery
- 변환
- 납입
- 급성심근경색증
Archives
- Today
- Total
SeouliteLab
[Java/자바] 디렉토리(폴더) 생성하는 3가지 방법 본문
Java에서 디렉토리(폴더)를 생성하는 다양한 방법을 알아보겠습니다. 각 방법은 예제와 함께 자세히 설명됩니다.
예제 1: File 객체의 mkdir() 메서드 사용
import java.io.File;
public class DirectoryCreationExample {
public static void main(String[] args) {
String directoryPath = "C:\\example_directory";
File directory = new File(directoryPath);
if (!directory.exists()) {
directory.mkdir();
System.out.println("디렉토리 생성됨: " + directoryPath);
} else {
System.out.println("디렉토리 이미 존재함: " + directoryPath);
}
}
}
File 클래스의 mkdir() 메서드를 사용하여 디렉토리를 생성합니다. 먼저 디렉토리가 존재하는지 확인하고, 존재하지 않을 경우 mkdir() 메서드를 호출하여 디렉토리를 생성합니다.
예제 2: Files 클래스의 createDirectory() 메서드 사용
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
public class DirectoryCreationExample {
public static void main(String[] args) {
String directoryPath = "C:\\example_directory";
Path path = Paths.get(directoryPath);
try {
Files.createDirectory(path);
System.out.println("디렉토리 생성됨: " + directoryPath);
} catch (IOException e) {
System.out.println("디렉토리 생성 실패: " + e.getMessage());
}
}
}
Files 클래스의 createDirectory() 메서드를 사용하여 디렉토리를 생성합니다. 예외 처리를 통해 생성 실패 시 오류 메시지를 출력합니다.
예제 3: mkdir() 메서드와 Paths 클래스의 createDirectory() 메서드 사용
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
public class DirectoryCreationExample {
public static void main(String[] args) {
String directoryPath = "C:\\example_directory";
Path path = Paths.get(directoryPath);
try {
Files.createDirectories(path);
System.out.println("디렉토리 생성됨: " + directoryPath);
} catch (IOException e) {
System.out.println("디렉토리 생성 실패: " + e.getMessage());
}
}
}
Files 클래스의 createDirectories() 메서드를 사용하여 디렉토리를 생성합니다. 이 메서드는 상위 디렉토리가 없는 경우에도 모든 디렉토리를 생성합니다.
'프로그래밍' 카테고리의 다른 글
[Java/자바] ObjectMapper를 사용한 JSON 파일 읽기 및 쓰기 (0) | 2024.03.08 |
---|---|
[Java/자바] JSON 파일 읽고 쓰기 (0) | 2024.03.08 |
[Java/자바] 배열을 Stream으로 변환하는 3가지 방법 (0) | 2024.03.08 |
[Java/자바] 파일이 비어있는지 확인하는 방법 (0) | 2024.03.08 |
[Java/자바] 파일 이름에서 확장자 제거하기 (0) | 2024.03.07 |