黑马程序员技术交流社区
标题:
关于字节流复制文件
[打印本页]
作者:
优质码农
时间:
2015-4-23 00:31
标题:
关于字节流复制文件
package homework;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*8、编写程序,采用多种方式,把d:\\video01.avi的内容复制到d:\\video02.avi中
方式1:基本字节流一次读写一个字节
方式2:基本字节流一次读写一个字节数组
方式3:高效字节流一次读写一个字节
方式4:高效字节流一次读写一个字节数组
C:\Users\wkk\Desktop\Java\作业题\day17.doc
*/
public class HomeWork8 {
public static void main(String[] args) throws IOException {
File file = new File("C:\\Users\\wkk\\Desktop\\Java\\作业题\\day17.doc");
File file2=new File("123.doc");
//methord1(file,file2);
//methord(file,file2);
//methord2(file,file2);
methord3(file,file2);
}
private static void methord3(File file, File file2) throws IOException {
BufferedInputStream bis= new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file2));
byte[] buffer = new byte[100];
int len=-1;
while((len=bis.read(buffer))!=-1){
bos.write(buffer, 0, len);
}
}
private static void methord2(File file, File file2) throws IOException {
BufferedInputStream bis= new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file2));
int ch = -1;
while((ch=bis.read())!=-1){
bos.write(ch);
}
bos.close();
bis.close();
}
private static void methord(File file, File file2) throws IOException {
FileInputStream fis =new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(file2);
byte[] b = new byte[100];
int len=-1;
while((len=fis.read(b))!=-1){
fos.write(b, 0, len);
}
fis.close();
fos.close();
}
private static void methord1(File file, File file2) throws IOException {
FileOutputStream fos=new FileOutputStream(file2);
FileInputStream fis = new FileInputStream(file);
int ch = -1;
while ((ch=fis.read())!=-1) {
fos.write(ch);
}
fis.close();
fos.close();
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2