本帖最后由 许聪聪 于 2013-6-12 17:46 编辑
我自己写的,希望对楼主有用
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- /**
- * 编写程序拷贝一个文件, 尽量使用效率高的方式.
- */
- public class Test6{
-
- public static void main(String[] args) throws IOException {
- String src_file = "E:/java/test.doc";
- String des_file = "E:/java/test_copy.doc";
-
- copyFile(src_file, des_file);
-
- System.out.println("OK!");
- }
-
- public static void copyFile(String src, String des) throws IOException {
- BufferedInputStream inBuff = null;
- BufferedOutputStream outBuff = null;
- try {
- // 新建文件输入流并对它进行缓冲
- inBuff = new BufferedInputStream(new FileInputStream(src));
- // 新建文件输出流并对它进行缓冲
- outBuff = new BufferedOutputStream(new FileOutputStream(des));
- // 缓冲数组
- byte[] b = new byte[600 * 5];
- int len;
- while ((len = inBuff.read(b)) != -1) {
- outBuff.write(b, 0, len);
- }
- // 刷新此缓冲的输出流
- outBuff.flush();
- } finally {
- // 将流关闭
- if (inBuff != null)
- inBuff.close();
- if (outBuff != null)
- outBuff.close();
- }
- }
- }
复制代码 |