티스토리 뷰
java.io.File - 파일 Directory 구별 및 파일 리스트 표시.
File ojbect 내의 isDirectory function 를 이용한 지정 Directory 내의 파일및 디렉토리 리스트를 표시하는 방법이다. 웹 개발시에도 파일 업로드 및 삭제 작업시 자주 사용 된다. 더 많은 응용이 필요하겠지만...
Example
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
'Java' 카테고리의 다른 글
| [Java] InputStreamReader 와 OutputStreamWriter 를 이용한 파일 내용 출력 (0) | 2016.08.08 |
|---|---|
| [Java] FileInputStream 과 FileOutputStream를 이용한 파일 복사 (0) | 2016.08.07 |
| [Java] FileInputStream 를 이용한 파일 내용 출력 (0) | 2016.08.07 |
| [Java] File클래스 Temp 파일 생성하기 (0) | 2016.08.06 |
| [Java] 파일 존재 유무 확인 (Checking for a file's existence) (1) | 2016.08.04 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- FileInputStream
- catch
- Rowcount
- Field
- foreach
- BufferedReader
- FOUND_ROW
- 임시파일
- jstl
- highlight.js
- IF
- select
- isDirectory
- reflection
- IO
- java
- 소스코드
- BufferedWriter
- Set
- InputStreamReader
- remove
- choose
- JSP
- InputStreamWriter
- forToken
- file
- 파일
- 자바
- 라이브아카데미
- syntax
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
글 보관함