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;
}
}
}
本人是菜鸟一个,希望看到这个方法的人能发表自己的看法,最好提出建议啊!!!

你可能感兴趣的:(java写的一个用字节流复制文件的方法)