package IO流;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Copy {
    public static void main(String[] args) {
        
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("E://aa.txt");
             fos = new FileOutputStream("E://bb.txt");
            
            /**
             * 自定义缓存区对象
             */
            byte[] bt = new byte [1000000];
            
            int by = 0;
        
                while ((by = fis.read(bt) )!= -1){
                    
                    fos.write(bt, 0, by);
                }
                    
            
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        
        finally{
            try {
                fis.close();
                fos.close();
            }
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        
            
        }
    }
}

关于 “( by = fis.read(bt)) != -1 解释

查看源码发现read方法返回的是

  public int read(byte b[]) throws IOException {
        return readBytes(b, 0, b.length);
    }
当读取完文件最后一个字节返回的是-1;

 

posted on 2017-07-09 12:58  慕星流  阅读(139)  评论(0编辑  收藏  举报