`
zhaohong
  • 浏览: 59311 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

JAVA复制文件(用字节流实现)

阅读更多
package com.test.file;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class copyFile {

	public static void main(String[] args) {

		BufferedInputStream bi = null;
		FileOutputStream fo = null;
		String s = null;
		try {
			bi = new BufferedInputStream(new FileInputStream(new File(
					"D:\\a.docx")));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		try {
			fo = new FileOutputStream(new File("d:\\b.docx"));

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		byte[] content = new byte[1000];
		int size = 0;
		try {
			while ((size = bi.read(content)) != -1) {
				fo.write(content, 0, size);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (bi != null) {
					bi.close();
				}
				if (fo != null) {
					fo.close();
				}
			} catch (Exception e) {
			}
		}
	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics