티스토리 뷰

IT/JAVA

자바 파일 복사

김보야 2017. 3. 27. 11:45

channel, buffer, stream을 이용한 파일 복사




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
    /**
     * FileChannel을 이용한 복사
     * @param strSourcePath(소스 경로)
     * @param strDestinationPath(목적 경로)
     * @param strFileName(파일명)
     */
    public static void fileChannelCopy(String strSourcePath, String strDestinationPath, String strFileName) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        FileChannel fcIn = null;
        FileChannel fcOut = null;
        
        try {
            fis = new FileInputStream(strSourcePath+strFileName);
            fos = new FileOutputStream(strDestinationPath+strFileName);
            
            fcIn = fis.getChannel();
            fcOut = fos.getChannel();
            
            long lSize = fcIn.size();
            fcIn.transferTo(0, lSize, fcOut);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
            try {
                if (fcIn != null) {
                    fcIn.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
            try {
                if (fcOut != null) {
                    fcOut.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }
cs





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
    /**
     * Buffer를 이용한 복사
     * @param strSourcePath(소스 경로)
     * @param strDestinationPath(목적 경로)
     * @param strFileName(파일명)
     */
    public static void fileBufferCopy(String strSourcePath, String strDestinationPath, String strFileName) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        
        try {
            fis = new FileInputStream(strSourcePath+strFileName);
            fos = new FileOutputStream(strDestinationPath+strFileName);
            
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            
            int iLen = 0;
            int iSize = 1024;
            byte[] bBuf = new byte[iSize];
            while ((iLen = bis.read(bBuf, 0, iSize)) != -1) {
                bos.write(bBuf, 0, iLen);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
            try {
                if (bis != null) {
                    bis.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
            try {
                if (bos != null) {
                    bos.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }
cs





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
    /**
     * Stream을 이용한 복사
     * @param strSourcePath(소스 경로)
     * @param strDestinationPath(목적 경로)
     * @param strFileName(파일명)
     */
    public static void fileStreamCopy(String strSourcePath, String strDestinationPath, String strFileName) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        
        try {
            fis = new FileInputStream(strSourcePath+strFileName);
            fos = new FileOutputStream(strDestinationPath+strFileName);
            
            int iLen = 0;
            byte[] bBuf = new byte[1024];
            while ((iLen = fis.read(bBuf)) != -1) {
                fos.write(bBuf, 0, iLen);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }
cs


'IT > JAVA' 카테고리의 다른 글

Unsupported major.minor version  (0) 2017.09.20
자바 메소드명, 클래스명, 줄번호, 파일명 가져오기  (0) 2017.09.14
AES256 암복호화  (0) 2016.03.29
SDK, JRE, JDK  (0) 2016.03.28
JAVA SE, EE, ME  (0) 2016.03.28
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/01   »
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
글 보관함