• 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("出错了");
                    }
                    
                }
            }
            
        }
    
    }
  • 相关阅读:
    C# 开发Chrome内核浏览器(WebKit.net)
    SQL 存储过程语句拼接愁人的时间类型
    Linux简介
    揭秘Node.js深受欢迎的原因
    6个强大的AngularJS扩展应用
    ElasticSearch NEST
    web中的各种打印方案
    配置问题总结
    leetcode -day29 Binary Tree Inorder Traversal & Restore IP Addresses
    HBase高速导入数据--BulkLoad
  • 原文地址:https://www.cnblogs.com/yangyongqian/p/5153198.html
Copyright © 2020-2023  润新知