package 자바;
import java.io.File;
import java.io.IOException;
public class ex02 {
public static void main(String[] args) {
String fileName = "test.txt";
File file = new File(fileName);
try {
file.createNewFile(); // 파일 생성
if(file.exists()) {
System.out.println("파일 이름 : " + file.getName());
System.out.println("파일 절대 경로 : " + file.getAbsolutePath());
System.out.println("파일 크기 (Byte) : " + file.length());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
결과
파일 이름 : test.txt
파일 절대 경로 : C:\Users\DH\eclipse-workspace\자바\test.txt
파일 크기 (Byte) : 11
package 자바;
import java.io.File;
public class ex03 {
public static void main(String[] args) {
String folderName = "A";
File folder = new File(folderName);
folder.mkdir(); // 폴더 만들기
if(folder.exists()) {
System.out.println("폴더가 존재합니다 : " + folder.getAbsolutePath()); // 절대경로
}
}
}
결과
폴더가 존재합니다 : C:\Users\DH\eclipse-workspace\자바\A

실제로 txt파일과 폴더가 만들어졌다.
folderName = "A/B/C";
folder = new File(folderName);
folder.mkdirs(); // 여러개의 하위폴더를 만들려면 -s를 붙여줘야함.
if(folder.exists()) {
System.out.println("폴더가 존재합니다 : " + folder.getAbsolutePath()); // 절대경
} else {
System.out.println("폴더 생성 실패 (mkdirs)");
}
결과
폴더가 존재합니다 : C:\Users\DH\eclipse-workspace\자바\A\B\C
A 밑에 B 밑에 C 폴더 존재