close


分享寫一個複製(copy)程式

package com.test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class CopyTest {

public static void main(String[] args) {
CopyTest ct = new CopyTest();
try {
ct.copy(new File("D:/test.txt"), new File("D:/test1.txt"));
} catch (Exception e) {
e.printStackTrace();
}
}

public static int BUFFER_SIZE = 1024;

/**
* 進行檔案複製
*
* @param src
* 來源檔案
* @param dst
* 目的端
*/
public void copy(File src, File dst) throws Exception {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[Integer.parseInt(String.valueOf(src
.length()))];
while (in.read(buffer) > 0) {
out.write(buffer);
}/* end of while */
} finally {
try {
in.close();
} catch (Exception e0) {
}
try {
out.close();
} catch (Exception e1) {
}
in = null;
out = null;
}/* end of try-catch */
}

}







 















其它文章

arrow
arrow
    文章標籤
    java io copy
    全站熱搜

    PG Levin Li 發表在 痞客邦 留言(0) 人氣()