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
- 설치방법
- 예제
- 자바공부
- OOP
- 자바객체
- 자바설치
- 자바I/O
- 서블릿 값 가져오기
- 객체
- 서블릿 동작방식
- 숫자야구
- 조건문
- 자바
- 테이블 설계
- 스트림
- 입출력
- 자바 암호화
- 서블릿 파라미터
- 서블릿
- 컨텍스트 초기화 객체
- File
- 자바기초
- 서블릿 예제
- 스택
- 오라클
- Servlet
- SQL
- DA#
- 자바문제
- java
Archives
- Today
- Total
다양한 관심 :)
JAVA - FILE 본문
MAIN : 보고싶은 디렉토리를 설정하기
public static void main(String[] args) {
A_FileTest test = new A_FileTest();
File viewFile = new File("d://D_Other");
//보고싶은 디렉토리 설정, ()안에만 원하는대로 바꿔서 가능
test.displayFileList(viewFile);
}
디렉토리(폴더)를 매개값으로 받아서
해당 디렉토리(폴더)에 있는 모든 파일과 디렉토리(폴더)목록을 출력하는 메서드 만들기
public void displayFileList(File dir) {
if(!dir.isDirectory()) {
System.out.println("디렉토리(폴더)만 가능합니다.");
return;
}
System.out.println("[" + dir.getAbsolutePath()+ " ] 디렉토리 내용 ");
System.out.println();
//file 객체
File[] files = dir.listFiles();
//년-월-일 오전/오후 시간:분
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd a hh:mm");
for(int i=0; i<files.length; i++) {
String fileName = files[i].getName(); //파일의 이름 저장
String attr = " "; //파일의 속성(읽기, 쓰기, 히든, 디렉토리를 구분)
String size = " "; // 파일의 크기
if(files[i].isDirectory()) {
attr = "<DIR>";
}else {
size = files[i].length() + " ";
attr = files[i].canRead() ? "R" : "";
attr = files[i].canWrite() ? "W" : "";
attr = files[i].isHidden() ? "H" : "";
}
System.out.printf("%s %5s %12s %s\n",
df.format(new Date(files[i].lastModified())),
attr, size, fileName );
}
- File 객체 안에는 디렉토리가 들어와야 한다.
- 전체목록을 가져올 때 에는 ( File의 배열 / String배열 사용가능)
//1.file 객체
File[] files = dir.listFiles();
//2. String 배열 사용
String [] filestrs = dir.list();
- 파일이름의 저장 : getName( );
- 파일의 속성 구분 ex) 읽기가 가능하면 r 아니면 공백 의미
- last modified : 파일의 마지막 수정날짜를 가져온다.
- 파일의 크기 저장 : length( );
canRead() ? "R" : " " ; : 읽기 속성
canWirte() ? "W" : " "; : 쓰기 속성
isHidden() ? "H" : " "; : 히든 (숨긴파일)
- printf 사용하여 출력 포맷
printf : 서식 지정자를 통해 출력할 데이터의 서식을 지정
(지시자를 통해 변수의 값을 여러 가지 형식으로 변환하여 출력)
=> %와 어떤 지시자의 단어사이에 숫자가 들어가면 그 숫자만큼 출력칸이 생기고 출력칸의 오른쪽부터 채워진다.
- %s : 문자열(String)로 출력
- 12개의 글자수 : 글자수가 적으면 공백이 출력, 글자수가 많으면 12글자로 잘려서 출력
- %n : 줄바꿈 기능
System.out.printf("%s %5s %12s %s\n",
df.format(new Date(files[i].lastModified())), attr, size, fileName );
콘솔 출력 결과
'프로그래밍 공부 > JAVA예제' 카테고리의 다른 글
JAVA - File I/O (인코딩) (0) | 2020.11.09 |
---|---|
JAVA- Collection (LIST SORT) (0) | 2020.11.03 |
숫자 야구 프로그램 (0) | 2020.10.26 |
로또 프로그램 (0) | 2020.10.26 |
RESTAURANT (0) | 2020.10.12 |