字节流复制文件

直接复制

1
2
3
4
5
6
7
8
9
10
11
12
13
FileInputStream fis = new FileInputStream("smile.mp3");
FileOutputStream fos = new FileOutputStream(".\music\smile.mp3");
byte[] buf = new byte[1024];
int len = 0;
while((len=fis.read(buf))!=-1){
fos.write(buf,0,len);
}
fos.close();
fis.close();

使用缓冲区复制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
FileInputStream fis = new FileInputStream("smile.mp3");
BufferedInputStream bufis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(".\music\smile.mp3");
BufferedOutputStream bufos = new BufferedOutputStream(fos);
int ch = 0;
while((ch=bufis.read())!=-1){
bufos.write(ch);
}
bufos.close();
bufis.close();