ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JAVA/자바] 디렉토리 복사
    Language/Java 2018. 4. 28. 18:45
    반응형


    디렉토리 복사

    Directory Copy

    만들고 싶은 기능

    1. 사용자가 직접 경로 입력 ( 대상 / 위치 )

    2. 폴더안의 내용 모두를 복사한다.


    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
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    import java.io.*;
     
    public class FileCopy {
        FileInputStream fis;
        FileOutputStream fos;
        
        BufferedReader br;
        
        String sF , tF;
        
        FileCopy(){
            br = new BufferedReader(new InputStreamReader(System.in));
            try{
                System.out.println("복사할 파일의 디렉토리 주소를 입력하여 주세요 \nex) C:\\STUDY");
                sF = br.readLine();
                System.out.println("복사할 파일이 복사될 주소를 입력하여 주세요 \nex) D:\\CIVIC");
                tF = br.readLine() + "\\copy";
                
                File sourceF = new File(sF);
                File targetF = new File(tF);
                
                copy(sourceF , targetF);
            }catch(Exception e) {}
        }
        
        // 폴더를 복사하는 
        public void copy(File sourceF , File targetF) {
            targetF.mkdir();
            File[] fileList = sourceF.listFiles();
            
            for(File file : fileList) {
                File temp = new File(targetF.getAbsolutePath() + File.separator + file.getName());
                
                if(file.isDirectory()) {
                    temp.mkdir();
                    copy(file , temp);
                }else {
                    try {
                        fis = new FileInputStream(file);
                        fos = new FileOutputStream(temp);
                        
                        byte[] b = new byte[4096];
                        int count = 0;
                        
                        while((count = fis.read(b)) != -1) {
                            fos.write(b , 0 , count);
                        }
                        
                        System.out.println("파일 복사 성공");
                    }catch(Exception e) {
                        System.out.println("파일 복사 실패");
                        e.printStackTrace();
                    }finally {
                        try {
                            fis.close();
                            fos.close();
                        }catch(Exception e) {}
                    }
                }
            }
        }
        
        public static void main(String[] args) {
            FileCopy fc = new FileCopy();
        }
    }
     
    cs


     


    [ C:\civic 폴더 ] 에는 method 폴더의 3개의 java 파일 + JAVA-ICON 이라는 이미지 파일이 있습니다.


    그이후 만들어 놓았던 복사될 주소 [ C:\ccc 폴더 ] 를 입력하면 들어가는 것을 확인 할 수 있습니다.


     

    < 복사가 완료된 것을 확인 할 수 있다. >


      


    그냥 엔터 칠 경우 

    Default 값으로 + "\\copy" 를 했기 때문에 

    [ C:\copy 폴더] 가 생성되는 것 또한 확인할 수 있다. 



    "\\" 을 2번 해주는 이유는 


    "\n" , "\t" 등등 하나의 "\" 을 입력하는 경우에

    \가 생략되는 경우가 있기 때문이다.


    참고

    1. IO 입출력 ( BufferedReader / FileInputStream )

    2. File

    3. 예외 처리 ( try{}catch(){} )

    4. 반복문 ( for / while )

    5. 조건문 ( if(){}else{} )


    반응형
Designed by Tistory.