JAVA IO 字节流 FileInputStream FileOutputStream

摘抄自 b站尚硅谷JAVA视频教程

 

与字符流操作基本一致.这里给出使用字节流复制一张图片的代码.

File file = null;
        File gg = null;
        gg = new File("gg.jpg");
        file = new File("ggCopy.jpg");
        FileOutputStream fo=null;
        FileInputStream fi =null;
        try {
            fo = new FileOutputStream(file);
            fi = new FileInputStream(gg);

            byte [] bytes = new byte[5];
            int len =0;
            while ((len=fi.read(bytes))!=-1){
                fo.write(bytes,0,len);
                ;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fo!=null){
                try{
                    fo.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            if(fi!=null){
                try{
                    fi.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }

 

posted @ 2020-02-21 11:51  超级学渣渣  阅读(155)  评论(0编辑  收藏  举报