A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 狂飙的yellow.co 于 2013-6-1 19:30 编辑

我对于IO流复制文件,总结了四种方法
1.第一种复制方法,使用自定义的缓冲区
  1. public static void copyfile_1() throws IOException{//抛出异常
  2.                 //其中file的 \\是转义字符
  3.                 FileInputStream in = new FileInputStream("E:\\yellowcong.txt");//输入流
  4.                 FileOutputStream out = new FileOutputStream("E:\\heima.txt");//输出流
  5.                
  6.                 //自定义缓冲区
  7.                 byte [] buf = new byte[1024];
  8.                
  9.                 int len = 0;//用于判断是否写入完成的标记
  10.                 //多次的重复读写数据
  11.                 while((len = in.read(buf))!= -1){//读取数据,写入缓冲区
  12.                         out.write(buf, 0, len); // 写出数据
  13.                 }
  14.                
  15.                 //关闭资源
  16.                 out.close();
  17.                 in.close();
  18.         }
复制代码
第二种方法 使用java提供的缓冲区,这种方法的效率高一点
  1. public static void copyfile_2() throws IOException {
  2.                 // TODO Auto-generated method stub
  3.                 //读取数据的缓冲区
  4.                 BufferedInputStream in = new BufferedInputStream(new FileInputStream("E:\\yellowcong.txt"));
  5.                
  6.                 //写数据的缓冲区
  7.                 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("E:\\heima.txt"));
  8.                
  9.                 int ch = 0;//其中这个ch 不是用来标记的,而是来写的
  10.                 while((ch = in.read())!= -1){
  11.                         out.write(ch);
  12.                 }
  13.                
  14.                
  15.                 //关闭资源
  16.                 in.close();
  17.                 out.close();
  18.         }
复制代码
第三种方法:使用 aviable() 获取数据的大小,然后定义和复制的文件一样大的缓冲区
  1. public static void copyfile_3() throws IOException{//抛出异常
  2.                 //其中file的 \\是转义字符
  3.                 FileInputStream in = new FileInputStream("E:\\yellowcong.txt");//输入流
  4.                 FileOutputStream out = new FileOutputStream("E:\\heima.txt");//输出流
  5.                
  6.                 //自定义缓冲区
  7.                 byte [] buf = new byte[in.available()];
  8.                
  9.                 in.read(buf);
  10.                 out.write(buf);
  11.                 //关闭资源
  12.                 out.close();
  13.                 in.close();
  14.         }
复制代码
第四中方法    不用缓冲区的方法
  1. public static void copyfile_4() throws IOException{//抛出异常
  2.                 //其中file的 \\是转义字符
  3.                 FileInputStream in = new FileInputStream("E:\\yellowcong.txt");//输入流
  4.                 FileOutputStream out = new FileOutputStream("E:\\heima.txt");//输出流
  5.                
  6.                 int ch = 0;//用于读写, 读一个写一个,速度是很慢
  7.                 //多次的重复读写数据
  8.                 while((ch = in.read())!= -1){//读取数据,写入缓冲区
  9.                         out.write(ch); // 写出数据
  10.                 }
  11.                
  12.                 //关闭资源
  13.                 out.close();
  14.                 in.close();
  15.         }
复制代码

评分

参与人数 1技术分 +1 收起 理由
Sword + 1

查看全部评分

7 个回复

倒序浏览
心都碎了,居然没有一个人来看一下。。。。。。。。
回复 举报
我看还是的顶一下啊
回复 举报
童鞋,这个比较的全面
回复 举报

童鞋,我看的顶你一下,不然你会崩溃的
回复 举报
我看行,这个真还是可以的
回复 举报
辛苦了,顶一个
回复 举报
学习一下,感谢分享!
回复 举报
您需要登录后才可以回帖 登录 | 加入黑马