zoukankan      html  css  js  c++  java
  • java基础知识回顾之javaIO类---FileInputStream和FileOutputStream字节流复制图片

    package com.lp.ecjtu;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    
    /**
     * 
     * @author Administrator
     * 1.用字节读取流对象和图片相关联(输入流)
     * 2.用字节写入流对象创建一个图片文件。用于存储获取到的图片数据(输出流)
     * 3.通过循环读写,完成数据的储存
     * 4.关闭资源
     *
     */
    public class CopyPicStream {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
                fis = new FileInputStream("d:\1.jpg");//读取图像数据之类的原始字节流
                fos = new FileOutputStream("2.bmp");//用于写入诸如图像数据之类的原始字节流
                byte[] b = new byte[1024];
                int len = 0;
                while ((len=fis.read(b)) != -1){
                    fos.write(b);
                }
            }catch (FileNotFoundException e) {
                e.printStackTrace();
            }catch (IOException e) {
                throw new RuntimeException("复制图片失败!");
            }finally{
                try {
                    if(fis != null){
                        fis.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if(fos != null){
                        fos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
    }
     
  • 相关阅读:
    截屏 多难未遂
    捕捉异常
    Android中缓存记忆
    Android中的线程池
    悄悄为Android中解决部分适配问题哦!
    java中的服务端跳转与客户端跳转区别与联系
    doget(),doput()方法的使用
    基本概念--同步,异步
    java--SimpleDataFormat类
    java--9
  • 原文地址:https://www.cnblogs.com/200911/p/3955373.html
Copyright © 2011-2022 走看看