java写的一个用字节流复制文件的方法

 public void CopyBigFile(String to, String from) throws IOException {

File f = null;
FileInputStream fr = null;
FileOutputStream fw = null;
BufferedInputStream bi = null;
BufferedOutputStream bo = null;
File fd = null;
try {
f = new File(from);
fr = new FileInputStream(f);
bi = new BufferedInputStream(fr);
int index = to.lastIndexOf('/');
String dd = to.substring(0, index);
fd = new File(dd);
if (!fd.exists()) {
fd.mkdirs();
}
File ff = new File(fd, to.substring(index));
fw = new FileOutputStream(ff);
bo = new BufferedOutputStream(fw);
byte[] temp = new byte[1024];
while (bi.available() != 0) {
bi.read(temp);
System.out.println(bi.available());
bo.write(temp);
}
bo.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
throw e;
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
if (bi != null) {
try {
bi.close();
} catch (IOException e) {
e.printStackTrace();
throw e;
}
}
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
throw e;
}
}
try {
bo.close();
fr.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
throw e;
}
}
}
本人是菜鸟一个,希望看到这个方法的人能发表自己的看法,最好提出建议啊!!!
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值