IO流实例--------------复制图片

IO流复制图片的本质操作

在这里插入图片描述
代码如下:
先使用InputStream读取图片,再使用OutputStream写入

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class CopyImage {
    
    

	public static void main(String[] args) {
    
    
		int i = 0;
		InputStream is = null;
		OutputStream os = null;
		try {
    
    
			is = new FileInputStream("/Users/wx二维码.jpg");
			os = new FileOutputStream("/Users/wx测试.jpg");
			while ((i = is.read()) != -1) {
    
    
				char ch = (char) i;
//				System.out.println(ch);
				os.write(ch);
			}
		} catch (IOException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
    
    
			try {
    
    
				is.close();
				os.close();
			} catch (IOException e) {
    
    
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_41907283/article/details/131063223