io流使用案例---复制图片

版权声明:转载请注明出处 https://blog.csdn.net/doubleguy/article/details/87071466

这里我们用io流实现将输入流指定路径下的图片复制到输出流指定路径

代码如下:

package com.test9;

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

public class photoCopy {
    public static void main(String [] args){
        //定义输入输出流
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            //为输入输出流指定路径
            fis = new FileInputStream("C:\\Users\\zhangbin\\Pictures\\Camera Roll" +
                    "\\1c950a7b02087bf41df462a9fed3572c11dfcf51.jpg");
            fos = new FileOutputStream("D:\\a.jpg");
            //实际读入字节数
            int n = 0;
            //相当于缓存
            byte []bytes = new byte[1024];
            //循环写入
            while ((n=fis.read(bytes))!=-1){
                fos.write(bytes);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                //关闭输入输出流
                fis.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/doubleguy/article/details/87071466