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();
                }
            }
        }

猜你喜欢

转载自www.cnblogs.com/superxuezhazha/p/12340786.html