zoukankan      html  css  js  c++  java
  • IO流 拷贝图片

    package com.yyq;
    import java.io.*;
    /*
     * 复制一个图片
     * 思路: 1.用字节读取流对象和图片关联
     *      2.用字节写入流对象创建一个图片文件,用于存储获取到的图片数据
     *      3.通过循环读写,完成数据的存储
     *      4.关闭资源
     *      // 不用字符流拷贝媒体文件。
     * 
     */
    public class CopyPic {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try{
                fis = new FileInputStream("1.jpg");
                fos = new FileOutputStream("D:\1.jpg");
                byte[] buf = new byte[1024];
                int num = 0;
                while((num = fis.read(buf))!=-1){
                    fos.write(buf);
                }
            }
            catch(Exception e){
                throw new RuntimeException("出错了");
            }
            finally{
                if(fis!=null){
                    try{
                        fis.close();
                    }
                    catch(Exception e){
                        throw new RuntimeException("出错了");
                    }
                    
                }
                if(fos!=null){
                    try{
                        fos.close();
                    }
                    catch(Exception e){
                        throw new RuntimeException("出错了");
                    }
                    
                }
            }
            
        }
    
    }
    package com.yyq;
    import java.io.*;
    /*
     * 复制一个图片
     * 思路: 1.用字节读取流对象和图片关联
     *      2.用字节写入流对象创建一个图片文件,用于存储获取到的图片数据
     *      3.通过循环读写,完成数据的存储
     *      4.关闭资源
     *      // 不用字符流拷贝媒体文件。
     * 
     */
    public class CopyPic {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try{
                fis = new FileInputStream("1.jpg");
                fos = new FileOutputStream("D:\1.jpg");
                byte[] buf = new byte[1024];
                int num = 0;
                while((num = fis.read(buf))!=-1){
                    fos.write(buf);
                }
            }
            catch(Exception e){
                throw new RuntimeException("出错了");
            }
            finally{
                if(fis!=null){
                    try{
                        fis.close();
                    }
                    catch(Exception e){
                        throw new RuntimeException("出错了");
                    }
                    
                }
                if(fos!=null){
                    try{
                        fos.close();
                    }
                    catch(Exception e){
                        throw new RuntimeException("出错了");
                    }
                    
                }
            }
            
        }
    
    }
  • 相关阅读:
    大学生自学网
    如何保证主从复制数据一致性
    CDN
    后端 线上 服务监控 与 报警 方案2
    利用 Gearman 实现系统错误报警功能
    增量部署和全量部署
    后端线上服务监控与报警方案
    简析TCP的三次握手与四次分手
    301 和 302 对 SEO 的影响
    Linux 查看负载
  • 原文地址:https://www.cnblogs.com/yangyongqian/p/5153198.html
Copyright © 2011-2022 走看看