글보기
[JAVA] 디렉토리 복사 예제(java.io)

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class CopyDir {
        public static void copyDirectory(File sourcelocation , File targetdirectory) 
    throws IOException {
            //디렉토리인 경우
            if (sourcelocation.isDirectory()) {               
                    //복사될 Directory가 없으면 만듭니다.
                if (!targetdirectory.exists()) {
                    targetdirectory.mkdir();
                }
                
                String[] children = sourcelocation.list();
                for (int i=0; i<children.length; i++) {
                    copyDirectory(new File(sourcelocation, children[i]),
                            new File(targetdirectory, children[i]));
                }
            } else {
                //파일인 경우
                InputStream in = new FileInputStream(sourcelocation);                
                        OutputStream out = new FileOutputStream(targetdirectory);
                
                // Copy the bits from instream to outstream
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                in.close();
                out.close();
            }
        }
        
    public static void main(String[] args) throws IOException {
            //c:\LOG의 내용을 c:\Temp에 복사 합니다.
            File source = new File("c:\\LOG");
            File target = new File("c:\\Temp");
            
            copyDirectory(source , target);
    }
    
}


아래는 관련 댓글들입니다.   댓글수: 0

앨범방 문의전화
010-2358-5700
평일 오전10시 ~ 오후5시
앨범방 계좌번호
하나은행
659-910429-20707
예금주 조우성
신용카드결제
안전한 나이스페이 전자결제시스템을 사용하고 있습니다.
이상 무료배송
주문하시는 총상품금액의 합계가 이상일 경우 택배비()가 무료입니다.
안전거래가맹점