티스토리 뷰

java.io.File - 파일 Directory 구별 및 파일 리스트 표시.

File ojbect 내의 isDirectory function 를 이용한 지정 Directory 내의 파일및 디렉토리 리스트를 표시하는 방법이다. 웹 개발시에도 파일 업로드 및 삭제 작업시 자주 사용 된다. 더 많은 응용이 필요하겠지만...

Example

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
package com.umejintan.file;
 
import java.io.File;
 
public class DirectoryFile {
 
    public static void main(String[] args) {
         
        // Create a file object
        File file = new File("c://Example//File");
         
        // 1. check if the file exists or not
        boolean isExists = file.exists();
         
        if(!isExists) {
            System.out.println("There is nothing.");
        }
         
        // 2. check if the object is directory or not.
        if(file.isDirectory()) {
            File[] fileList = file.listFiles();
            for(File tFile : fileList) {
                System.out.print(tFile.getName());             
                if(tFile.isDirectory()) {
                    System.out.print(" is ");
                    System.out.println("a directory.");
                } else {
                    System.out.print(" is ");
                    System.out.println("a file.");
                }
            }          
        } else {
            System.out.println("It is not a directory.");
        }
    }
}

Description

isDirectory 함수는 지성된 file 이 디렉토리인지 여부를 확인 하는데 이용된다. 

Reference


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/04   »
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
글 보관함