图片、文件的拷贝(IO流)

IO流,图片、文件的拷贝:

package util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Main
{
    public static void main(String[] args)
    {
        new Main().copyFile("C:\\Users\\Desktop\\JVM.txt", "C:\\Users\\Desktop\\JVM1.txt");
        new Main().copyPicture("C:\\Users\\Desktop\\aa.jpg", "C:\\Users\\Desktop\\bb.jpg");
    }

    /**
     * 字节流拷贝图片
     */
    public void copyPicture(String path1, String path2)
    {
        BufferedInputStream bis = null;
        BufferedOutputStream bos  = null;
        try
        {
            bis = new BufferedInputStream(new FileInputStream(path1));
            bos = new BufferedOutputStream(new FileOutputStream(path2));
            byte[] bs = new byte[10240];
            int len = 0;
            while((len = bis.read(bs)) != -1)
            {
                bos.write(bs, 0, len);
            }
            System.out.println("拷贝图片成功");
        }
        catch (FileNotFoundException e)
        {
            throw new RuntimeException(path1+" 文件不存在");
        }
        catch (IOException e)
        {
            throw new RuntimeException("拷贝图片失败");
        }
        finally
        {
            try
            {
                if(null != bis)
                {
                    bis.close();
                }
            }
            catch (IOException e)
            {
                throw new RuntimeException("bis关闭失败");
            }

            try
            {
                if(null != bos)
                {
                    bos.close();
                }
            }
            catch (IOException e)
            {
                throw new RuntimeException("bos关闭失败");
            }
        }
    }

    /**
     * 字符流、字节流都可以拷贝文件
     */
    public void copyFile(String path1,String path2)
    {
        BufferedReader br = null;
        BufferedWriter bw = null;
        try
        {
            //设置编码格式
            //br = new BufferedReader(new InputStreamReader(new FileInputStream(path1),"UTF-8"));
            //bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path2),"UTF-8"));
            br = new BufferedReader(new FileReader(path1));
            bw = new BufferedWriter(new FileWriter(path2));
            int len = 0;
            char[] cs = new char[10240];
            while((len = br.read(cs)) != -1)
            {
                bw.write(cs, 0, len);
            }
            System.out.println("拷贝文件成功");
        }
        catch (FileNotFoundException e)
        {
            throw new RuntimeException(path1+" 文件不存在");
        }
        catch (IOException e)
        {
            throw new RuntimeException("拷贝文件失败");
        }
        finally
        {
            try
            {
                if(null != br)
                {
                    br.close();
                }
            }
            catch(IOException e)
            {
                throw new RuntimeException("br关闭失败");
            }

            try
            {
                if(null != bw)
                {
                    bw.close();
                }
            }
            catch(IOException e)
            {
                throw new RuntimeException("bw关闭失败");
            }
        }

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值