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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

//关于这个使用带缓冲功能的字节流复制文件的小问题,有点错误想请教一下高手修改并提示一下错误的位置及原因,谢谢!

package jichu;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
//使用带缓冲功能的字节流复制文件
public class CopyFile {
  public static void main(String[] args) {
   BufferedReader bfr  =null;         
   BufferedWriter bfw  =null;
   try{
   bfr = new BufferedReader(new InputStreamReader(new FileInputStream(new File("c:\\2.txt")))) ;   
   bfw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\3.txt"))) ;
   byte[] bytes = new byte[1024];
   String length = null;
   while((length=bfr.readLine(bytes))!=null){
    bfw.write(bytes,0,length);
    bfw.flush();
   }
   }catch(IOException e){
    e.printStackTrace();
    throw new RuntimeException();
   }finally{
    if(bfr!=null)
     bfr.close();
     
   }finally{
    if (bfw!=null) {
     bfw.close();
    }
   }  
  }
}

评分

参与人数 1技术分 +1 收起 理由
李小然 + 1

查看全部评分

7 个回复

倒序浏览
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.io.OutputStreamWriter;
  8. //使用带缓冲功能的字节流复制文件
  9. public class CopyFile {
  10.   public static void main(String[] args) {
  11.    BufferedReader bfr  =null;         
  12.    BufferedWriter bfw  =null;
  13.    try{
  14.    bfr = new BufferedReader(new FileReader(new File("c:\\2.txt"))) ;   
  15.    bfw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\3.txt"))) ;
  16.    char bytes[] = new char[1024];
  17.    int length = 0;
  18.    while((length=bfr.read(bytes))>0){
  19.     bfw.write(bytes,0,length);
  20.     bfw.flush();
  21.    }
  22.    }catch(IOException e){
  23.     e.printStackTrace();
  24.     throw new RuntimeException();
  25.    }finally{
  26.     if(bfr!=null)
  27.                 try {
  28.                         bfr.close();
  29.                 } catch (IOException e) {
  30.                         // TODO Auto-generated catch block
  31.                         e.printStackTrace();
  32.                 }
  33.     if (bfw!=null) {
  34.         try {
  35.                         bfw.close();
  36.                 } catch (IOException e) {
  37.                         // TODO Auto-generated catch block
  38.                         e.printStackTrace();
  39.                 }
  40.        }
  41.    }
  42.   }
  43. }
复制代码
已修改,请查收~

评分

参与人数 1技术分 +1 收起 理由
李小然 + 1

查看全部评分

回复 使用道具 举报 1 0
本帖最后由 のソ夏末 于 2014-5-25 16:06 编辑
  1. package com.itheima;

  2. import java.io.*;

  3. //使用带缓冲功能的字节流复制文件
  4. public class CopyFile
  5. {
  6.         public static void main(String[] args)
  7.         {
  8.                 BufferedInputStream bis = null;
  9.                 BufferedOutputStream bos = null;
  10.                 try
  11.                 {
  12.                         FileInputStream fis = new FileInputStream("c:\\2.txt");
  13.                         bis = new BufferedInputStream(fis);
  14.                         FileOutputStream fos = new FileOutputStream("D:\\3.txt");
  15.                         bos = new BufferedOutputStream(fos);
  16.                         
  17.                         long len = bis.available();
  18.                         if(len > 1024*1024*200)
  19.                         {  
  20.                         int b;  
  21.                         while((b=bis.read()) != -1)
  22.                         {  
  23.                             bos.write(b);  
  24.                         }                     
  25.                         }
  26.                         else
  27.                         {  
  28.                         byte[] bytes = new byte[(int)len];        
  29.                         bis.read(bytes);  
  30.                         bos.write(bytes);
  31.                 }
  32.                 System.out.println("文件复制已经完成!");
  33.                 }
  34.                 catch (Exception e)
  35.                 {
  36.                         e.printStackTrace();
  37.                 }
  38.                 finally
  39.                 {
  40.                         if(bis != null)
  41.                         try
  42.                         {
  43.                                 bis.close();
  44.                         }
  45.                         catch (IOException e)
  46.                         {                        
  47.                                 e.printStackTrace();
  48.                         }
  49.                         
  50.                         if(bos != null)
  51.                         try
  52.                         {
  53.                                 bos.close();
  54.                         }
  55.                         catch (IOException e)
  56.                         {
  57.                                 e.printStackTrace();
  58.                         }
  59.                 }
  60.             
  61.         }
  62. }
复制代码
首先要分清字节流和字符流的区别?你的题目需求是说用字节流复制文件,而代码里面却使用字符流的相关方法在操作,这是其一。
其二,你所用字符流中的BufferedReader.readLine()方法,方法定义是读一行数据并存储在输入流缓冲区的字符数组中,所以不用你再去定义数组。
如果需要定义一个数组去保存读到的数据,那么对应的方法应该用read()方法。
其三,你定义的数组类型是字节数组,所用的流对象是字符流,题目是用字节流复制文件。所以前提还是搞清楚字节流、字符流的本质区别。
如果想把这个程序写得完美一些,应该把复制文件抽取成一个功能定义,而不是一股脑儿的写在主函数里面。
并且在复制前,需要进行一系列的判断:源文件是否存在?目标目录是否存在?目标目录下是否有同名文件?
自己补充上去吧!

评分

参与人数 1技术分 +1 收起 理由
轻语。 + 1

查看全部评分

回复 使用道具 举报 1 0
package exam;
//使用带缓冲功能的字节流复制文件。
import java.io.BufferedInputStream;  
import java.io.BufferedOutputStream;  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.text.DecimalFormat;
public class Test7
{  
        public static void copyFile(String file,String path){  
                BufferedInputStream bis = null;  
        BufferedOutputStream bos = null;  
        try
        {  
            File copyFile = new File(file);   
            File savePath = new File(path);  
            
            //如果要复制的文件不存在或者不是文件,发出提示并退出程序  
            if (!(copyFile.exists() && copyFile.isFile()))
            {  
                System.out.println("无效的文件,请检查");  
               System.exit(0);  
            }  
            //如果要保存到的目录不存在,则创建该目录   
            if (!(savePath.isDirectory())){  
                System.out.println("你指定的目录不存在,将自动创建!");  
                savePath.mkdirs();  
            }     
            //创建目标文件完整路径。  
           File saveFile = new File(savePath+"\\"+copyFile.getName());  
            //如果saveFile是一个文件,说明有同名文件存在,则提示并退出程序,避免覆盖同名文件。  
            if (saveFile.isFile())
            {  
                System.out.println("注意!该目录下有同名文件。");  
                System.exit(0);  
            }
          //创建输入流和输出流。  
           bis = new BufferedInputStream(new FileInputStream(copyFile));  
           bos = new BufferedOutputStream(new FileOutputStream(saveFile));  
            //获取输入流中的的字节数。  
            long len = bis.available();  
              
            //格式化显示文件大小,保留1位小数  
            DecimalFormat df = new DecimalFormat(".0");  
            String size = null;  
           if(len > 1024*1024*200){  
                System.out.println("要复制的文件超过200MB,不能复制!");  
            }else if(len > 1024*1024){  
                size = df.format(len/1024/1024) + " MB";  
             }else if(len > 1024){  
                size = df.format(len/1024) + " KB";  
            }else{  
                size = len + " 字节";  
            }  
            System.out.println("文件大小:" + size);  
            System.out.println("复制中...");  
              
            //记录复制开始时毫秒值  
            long start = System.currentTimeMillis();  
            
            //如果文件大于200MB,用一次读一个字节方式  
            //否则就用数组一次性读取方式复制  
            if(len > 1024*1024*200){  
               int by;  
                while((by=bis.read())!=-1){  
                    bos.write(by);  
                }  
            }else{  
               //创建数组缓冲区,指定大小为输入流中的的字节数len。  
                byte[] bytes = new byte[(int)len];        
               bis.read(bytes);    //从输入流中读取所有数据存储到数组中。  
                 bos.write(bytes);   //将数组中的所有数据写入的输出流缓冲区中。  
             }  
            //记录复制结束精辟时毫秒值  
              long end = System.currentTimeMillis();  
              
            System.out.println("复制完成");  
            System.out.println("耗时:"+(end-start)+"毫秒");  
        }  
        catch(IOException e){  
             throw new RuntimeException("复制文件失败");  
         }
        finally{  
                            try{  
                        if (bis!=null){  
                    bis.close();  
               }  
           }  
            catch(IOException e){  
               throw new RuntimeException("输出流关闭失败");  
           }  
            try{  
                if (bos!=null){  
                    bos.close();  
               }  
            }  

        

            catch(IOException e)
            {  
               throw new RuntimeException("输出流关闭失败");  
            }  
        }  
   }      
    public static void main(String[] args){  
        //源文件                    
            String copyFile = "D:\\新建文件夹\\刘熙峥\\I Love You.mpg";
            //指定目标路径,没有责创建
        String savePath = "E:\\黑马程序员";            
       copyFile(copyFile,savePath);  
    }  
}  
回复 使用道具 举报
学习了!!!!
回复 使用道具 举报
好1111111111111111111
回复 使用道具 举报
为明天而奋斗 来自手机 中级黑马 2014-10-12 06:57:31
7#
都是高手啊!
回复 使用道具 举报
已经学习,谢谢
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马