Java基础-流管道关闭—流.close();

简介:在finally{}中调用 流.close(); 保证流管道最后是关闭的。
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * @author czchina
 *
 */
public class TestStream {
    public static void main(String[] args){
        
        FileInputStream fis = null;
        FileOutputStream fos =null;
        
        try{
            fis = new FileInputStream("E:/Android/AndroidStudioProjects/text.txt");
            fos = new FileOutputStream("E:/Android/AndroidStudioProjects/store.txt");
            //生成一个字节数组
            byte [] buffer= new byte [500];
            while(true){
                int tmp = fis.read(buffer,0,buffer.length);
                if(-1 == tmp){
                    System.out.print("Read complete ! \n");
                    break;
                }
                fos.write(buffer,0,tmp);
            }
        }
        catch(Exception e){
            System.out.println(e);
        }
        
        finally{
            try{
                fis.close();
                fos.close();
            }
            catch(Exception e){
                System.out.println(e);
            }
        }
    }
}

console:

image

posted @ 2015-07-10 22:31  cuiz_book  阅读(945)  评论(0编辑  收藏  举报