JavaSE基础--文件的复制

package com.Day19.StudyTest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 * 文件的复制(举例:图片复制)
 *  分析:创建输入流和输出流
 *          输入流读取图片信息写入
 *          设置缓冲区
 *          输出流写出到指定路径
 *          资源释放
 * */
public class Demo4 {
    public static void main(String[] args) throws FileNotFoundException {
        //创建流对象
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("F:\\ff.jpg");
            fos = new FileOutputStream("D:\\mm.jpg");
            //创建缓冲区
            byte[] buffer = new byte[1024];
            int len ;
            while ((len = fis.read(buffer)) != -1){
                //把buffer的数据写入到fos对象的指定路径
                fos.write(buffer,0,len);
            }
        }
        catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            try {
                if(fis != null){
                    fis.close();
                }
                if(fos != null){
                    fos.close();
                }
            }
            catch (Exception e){
                System.out.println(e.getMessage());
            }
        }
        System.out.println("照片有两张了耶!");

    }
}

 

posted @ 2021-01-20 20:14  楼兰柔兰  阅读(45)  评论(0)    收藏  举报