티스토리 뷰

반응형
※ 일반 복사 방법 (JDK 1.4이전 IO 패키지 이용)
import java.io.*;
public static void copyFile(String source, String target) throws IOException {
    FileInputStream fis = new FileInputStream(source);
    FileOutputStream fos = new FileOutputStream(target);
    try {
        byte[] buf = new byte[1024];
        int i = 0;
        while ((i = fis.read(buf)) != -1) {
            fos.write(buf, 0, i);
        }
    } catch (IOException e) {
        throw e;
    } finally {
        if (fis != null)
            fis.close();
        if (fos != null)
            fos.close();
    }
}
※ 빠른 복사 방법 (JDK 1.4이상 NIO 패키지 이용)
import java.io.*;
import java.nio.channels.*;

public static void copyFile(String source, String target) throws IOException {
    FileChannel inChannel = new FileInputStream(source).getChannel();
    FileChannel outChannel = new FileOutputStream(target).getChannel();
    try {
        // magic number for Windows, 64Mb - 32Kb
        int maxCount = (64 * 1024 * 1024) - (32 * 1024);
        long size = inChannel.size();
        long position = 0;
        while (position < size) {
            position += inChannel.transferTo(position, maxCount, outChannel);
        }
    } catch (IOException e) {
        throw e;
    } finally {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }
}
반응형

'Web Dev > JAVA' 카테고리의 다른 글

[JAVA] Assertion의 설명과 문법  (0) 2013.04.30
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함