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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 飞龙传神 中级黑马   /  2015-8-20 20:59  /  432 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

复制文件
  步骤:
   a: 创建字节输入流和字节输出流对象
   b: 频繁的读写操作
   c: 释放资源
   
   // 第一次读取一个字节复制文件
   FileInputStream fis = new FileInputStream("a.txt") ;
   FileOutputStream fos = new FileOutputStream("b.txt") ;
   
   int by = 0 ;
   while((by = fis.read()) != -1){
    fos.write(by) ;
   }
   
   fos.close() ;
   fis.close() ;
   // 依次读取一个字节数组复制文件
   FileInputStream fis = new FileInputStream("a.txt") ;
   FileOutputStream fos = new FileOutputStream("b.txt") ;
   byte[] bytes = new byte[1024] ;
   int len = 0 ;
   while((len = fis.read(bytes)) != -1){
    fos.write(bytes , 0 , len) ;
   }
   fos.close() ;
   fis.close() ;
   
高效的字节输入流和字节输出流
  高效的字节输入流: BufferedInputStream
  高效字节输出流:   BufferedOutputStream

复制文件:
  // 一次读取一个字节复制文件
  BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.txt")) ;
  BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("b.txt")) ;
  
  int by = 0 ;
  while((by = bis.read()) != -1){
   bos.write(by) ;
  }
  
  bos.close() ;
  bis.close() ;
  
  // 依次读取一个字节数组复制文件
  BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.txt")) ;
  BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("b.txt")) ;
  
  byte[] bytes = new byte[1024] ;
  int len = 0 ;
  while((len = bis.read(bytes)) != -1){
   bos.write(bytes, 0, len) ;
  }
  
  bos.close() ;
  bis.close() ;

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马