일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 설치
- String
- kotlin #return #jump
- Java
- jsp
- File 클래스
- jQuery
- JavaScript
- 파일 입출력
- Event
- function
- property
- 상속
- 데이터베이스
- array
- Method
- 다형성
- 자바
- spring
- 오라클
- html
- css
- javaservlet
- 연락처 프로그램
- 연산자
- springframework
- 윈도우 #단축키
- list
- 코틀린#클래스#상속
- GUI
- Today
- Total
Binary World
JAVA CLASS : 파일 입출력(File 클래스) 03 - 폴더 생성, 삭제, 변경 본문
<파일 입출력 : 폴더 생성, 삭제, 변경>
* 폴더(디렉토리)/파일 생성, 삭제, 변경
* File 클래스:
- 파일(txt, doc, mp4, jpg, ..) 객체와 디렉토리(폴더) 객체를 다루기 위한 클래스
* File 클래스의 인스턴스를 생성 - new File();
- 메모리(Heap)에 File 클래스의 인스턴스를 생성한다는 의미
- 실제 하드디스크에 있는 물리적인 파일/폴더(디렉토리)를 생성하는 것은 아님!
- 실제 파일/폴더(디렉토리)를 만들기 위해서는 file 클래스의 메소드를 호출해야 함!
* 파일(디렉토리)의 경로: 절대 경로, 상대 경로
- 절대 경로: 루트(C:\, D:\, /)부터 파일(폴더)의 위치까지 전체 이름
(예) C:\Study\lab-java\java18-File
(예) /Users/user...
- 상대 경로: 현재 작업 디렉토리를 기준으로 파일(폴더)가 어디에 있는지를 표시
(예) temp\test.txt
.: 현재 디렉토리(폴더)
..: 상위 디렉토리(폴더)
- 파일 구분자(file separator) - 운영체제(OS)마다 다르게 사용
MS Windows - \ 사용
Unix, Linux, Android, ... = \ 사용
- 자바에서는 File.separator라는 static 변수를 제공
-> 운영 체제에 맞는 파일 구분자를 저장
<프로그램 코드>
FileMain14.java
package edu.java.file14; import java.io.File; public class FileMain14 { public static final String TEST_DIR1 = "C:\\test1"; // 절대 경로 public static final String TEST_DIR2 = "test2"; // 상대 경로 public static final String TEST_DIR3 = "C:" + File.separator + "Study" + File.separator + "test3"; public static void main(String[] args) { System.out.println(TEST_DIR3); System.out.println(TEST_DIR1); // 파일 폴더(디렉토리)를 다루기 위한 객체 File f = new File(TEST_DIR2); // exists(): 파일, 폴더(디렉토리)가 존재하는 지(true) 아닌지를 리턴 if (!f.exists()) {// 파일, 폴더가 없는 경우 System.out.println("폴더가 없습니다..."); // mkdir(): 디렉토리를 생성하는 메소드, 생성 성공하면 true를 리턴 if (f.mkdirs()) { System.out.println("폴더 생성 성공"); } else { System.out.println("폴더 생성 실패"); } } else { // 파일, 폴더 있는 경우 System.out.println("폴더가 이미 존재합니다"); } } // end main() } // end class FileMain14 |
* 출력화면
C:\Study\test3 C:\test1 폴더가 없습니다... 폴더 생성 성공 |
* FileMain15.java
package edu.java.file15; import java.io.File; import java.io.IOException; public class FileMain15 {
public static final String DIR_NAME = "test2"; public static final String FILE_NAME = "dummy.txt"; public static final String FILE_PATH = DIR_NAME + File.separator + FILE_NAME; public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(FILE_PATH);
// test2\dummy.txt 파일을 생성 File f = new File(FILE_PATH);
if(!f.exists()){ // 파일이 없는 경우 System.out.println("파일이 없습니다..."); // createNewFile(): 새로운 파일 생성, 성공하면 true 리턴 try { if(f.createNewFile()){ System.out.println("파일 생성 성공"); } else { System.out.println("파일 생성 실패"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { // 파일 있는 경우 System.out.println("파일이 존재합니다..."); } } } |
* 출력화면
test2\dummy.txt 파일이 없습니다... 파일 생성 성공 |
* FileMain16.java
package edu.java.file16; import java.io.File; public class FileMain16 {
public static final String TEST2 = "test2"; public static final String TEST3 = "test3"; public static final String DUMMY = "dummy.txt"; public static void main(String[] args) { // 파일, 디렉토리 이름 변경, 삭제 // test2 -> test3 디렉토리 이름 변경 File org = new File(TEST2); // 원본 디렉토리 File dest = new File(TEST3); // 변경할 디렉토리 // renameTo(): 파일, 디렉토리 이름 변경, 성공하면 true를 리턴 if (org.renameTo(dest)){ System.out.println("디렉토리 이름 변경 성공"); } else { System.out.println("디렉토리 이름 변경 실패"); }
// test3\dummy.txt String filePath = TEST3 + File.separator + DUMMY; File file = new File(filePath); // delete(): 파일, 디렉토리 삭제, 성공하면 true 리턴 if (file.delete()) { System.out.println("파일 삭제 성공"); } else { System.out.println("파일 삭제 실패"); } } // end main() } // end class FileMain16 |
* 출력화면
디렉토리 이름 변경 실패 파일 삭제 실패 |
* FileMain17.java
package edu.java.file17; import java.io.File; public class FileMain17 { public static void main(String[] args) { // 현재 디렉토리(current working directory) 정보 확인 String cwd = System.getProperty("user.dir"); System.out.println("CWD: " + cwd); // 현재 디렉토리에 대한 File 객체 File f = new File(cwd); // getName(): 파일, 디렉토리의 이름을 리턴 System.out.println("이름: " + f.getName()); // getPath(): 경로를 리턴 // File 객체를 생성할 때 // 1) 절대 경로로 생성했으면 절대 경로 이름을 리턴 // 2) 상대 경로로 생성했으면, 상대 경로 이름을 리턴 System.out.println("경로: " + f.getPath()); // getAblsolutePath(): 절대 경로를 리턴 System.out.println("절대 경로: " + f.getAbsolutePath()); // isDirectory(): File 객체가 디렉토리이면 true를 리턴 // isFile(): File 객체가 파일이면 true를 리턴 System.out.println("디렉토리? " + f.isDirectory()); System.out.println("파일? " + f.isFile());
System.out.println(); // listFiles(): 폴더(디렉토리)의 내용 확인 File[] list = f.listFiles(); for (File file : list) { if (file.isDirectory()) { System.out.print("<DIR>\t"); } else { System.out.print("<FILE>\t"); } System.out.print(file.getName() + "\t"); System.out.println(file.length() + "Bytes"); } } } |
* 출력화면
CWD: C:\Study\lab-java\Java18-File 이름: Java18-File 경로: C:\Study\lab-java\Java18-File 절대 경로: C:\Study\lab-java\Java18-File 디렉토리? true 파일? false <FILE> .classpath 301Bytes <FILE> .project 387Bytes <DIR> .settings 0Bytes <DIR> bin 0Bytes <DIR> src 0Bytes <DIR> temp 4096Bytes <DIR> test2 0Bytes <DIR> test3 0Bytes |
'개발자의 길 > JAVA' 카테고리의 다른 글
JAVA CLASS : 자바에 오라클 DB 연동하기(데이터 삽입) (0) | 2017.01.23 |
---|---|
JAVA CLASS : GUI (스윙) (0) | 2017.01.17 |
JAVA CLASS : 파일 입출력(File 클래스) 02 - Serializable (0) | 2017.01.16 |
JAVA CLASS : 파일 입출력(File 클래스) 01 - 기본 정의 및 사용 (0) | 2017.01.12 |
JAVA CLASS : 쓰레드(Thread) (0) | 2017.01.12 |