[Java] 입출력 관련 API/File 클래스
Day22-2 230530
입출력 관련 API
File 클래스
java.io 패키지에서 제공하는 File 클래스는 파일 및 폴더(디렉토리) 정보를 제공해주는 역할을 함.
File 클래스로부터 File 객체를 생성하려면 문자열 경로를 다음과 같이 제공해야 함.
File file = new File("C:/Temp/file.txt");
File file = new File("C:\\Temp\\file.txt");
해당 경로에 실제로 파일이나 폴더가 있는지 확인하고 싶다면 exist() 메소드를 호출하면 됨.
boolean isExist = file.exist();
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
32
33
34
35
36
37
38
39
40
41
|
package Ex;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
//File클래스 - 파일 및 폴더(디렉토리)정보를 제공해주는 역할
public class File_p641first {
public static void main(String[] args) throws IOException {
//File객체생성
File dir = new File("C:\\javaStudy\\myDir");
File file1 = new File("C:\\javaStudy\\myFile1.txt");
File file2 = new File("C:\\javaStudy\\myFile2.txt");
File file3 = new File("C:\\javaStudy\\myFile3.txt");
//물리적생성
if(dir.exists()==false) {dir.mkdirs();}
if(file1.exists()==false) {file1.createNewFile();}
if(file2.exists()==false) {file2.createNewFile();}
if(file3.exists()==false) {file3.createNewFile();}
//특정디렉토리안의 파일목록조회
File temp = new File("C:\\javaStudy");
File[] contents = temp.listFiles();
System.out.println("마지막수정일\t\t형태\t\t크기\t\t이름");
System.out.println("--------------------------------------------");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm a");
for(File f : contents) {
System.out.print(sdf.format(new Date(f.lastModified())));
if(f.isDirectory()) {
System.out.println("\t<DIR>\t"+f.length()+"\t\t\t"+f.getName());
}else {
System.out.println("\t\t\t"+f.length()+"\t\t\t\t"+f.getName());
}
}
}
}
|
cs |
*실행 결과
마지막수정일 형태 이름 크기
--------------------------------------------
2023-05-15 08:56 오전 <DIR> 4096 .metadata
2023-05-30 11:04 오전 25 1.txt
2023-05-30 19:04 오후 448 board.db
2023-05-30 09:30 오전 118226 car2.jpg
2023-05-30 18:22 오후 114688 car2_copy.jpg
2023-05-30 18:22 오후 118226 car_copy.jpg
2023-05-15 08:58 오전 <DIR> 4096 eclipse
2023-05-30 16:08 오후 <DIR> 0 myDir
2023-05-30 16:11 오후 0 myFile1.txt
2023-05-30 16:11 오후 0 myFile2.txt
2023-05-30 16:11 오후 0 myFile3.txt
2023-04-28 15:26 오후 <DIR> 0 pg
2023-05-26 09:27 오전 3 test.db
2023-05-25 17:46 오후 3 test.txt
2023-05-30 18:22 오후 21 test1.txt
2023-05-26 19:43 오후 7 test10.txt
2023-05-26 09:27 오전 3 test2.db
2023-05-26 09:27 오전 5 test3.db
2023-05-19 17:09 오후 <DIR> 4096 wk
2023-05-11 16:04 오후 <DIR> 4096 기타
2023-05-01 09:07 오전 <DIR> 4096 참고자료
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
package chap14;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class File {
//필드
//File객체생성
File dir = new File("C:\\javaStudy\\myDir");
File file1= new File("C:\\javaStudy\\myFile1.txt");
File file2= new File("C:\\javaStudy\\myFile2.txt");
File file3= new File("C:\\javaStudy\\myFile3.txt");
//생성자
//메서드
public static void main(String[] args) throws IOException {
//변수선언 및 객체생성
File_p641 obj = new File_p641();
obj.makeDirFile();//폴더 및 파일 (물리적)생성
obj.getFileList();//특정디렉토리안의 파일목록조회 호출
}//main
//폴더 및 파일 (물리적)생성
public void makeDirFile() throws IOException {
if(dir.exists()==false ) { dir.mkdirs(); }
if(file1.exists()==false) { file1.createNewFile();}
if(file2.exists()==false) { file2.createNewFile();}
if(file3.exists()==false) { file3.createNewFile();}
}
//특정디렉토리안의 파일목록조회
public static void getFileList() {
File temp = new File("C:\\javaStudy");
File[] contents = temp.listFiles();
System.out.println("마지막수정일\t\t형태\t크기\t이름");
System.out.println("--------------------------------------------");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm a");
for(File f: contents) {
System.out.print( sdf.format(new Date(f.lastModified())));
if(f.isDirectory()) {
System.out.println("\t<DIR>\t"+f.length()+"\t\t\t"+f.getName());
}else {
System.out.println("\t\t\t"+f.length()+"\t\t\t"+f.getName());
}
}
}
}
|
cs |
위의 코드와 같은 결과.