Use IO stream to copy the bytes of the picture output picture and copy the picture

package IOStraem;

import org.junit.Test;

import java.io.*;

/**
 * @Author:CT
 * @Date:2021/2/12
 * @Description:IOStraem
 * @Version 1.0
 */
/*
    字节流的使用:
    ① 相较于字符流 字节流可以处理图片 视频 等以字节形式存储的文件
    ② 使用方法与字符流类似
 */
public class IOStreamUse02 {
    
    
    @Test
    // 输出图片的字节码

    public void test(){
    
    
        File file=new File("0f61e4185b4ff57c170aaf96aa1a11e.jpg");//此处输入图片名称 需要在Moudle下 
        try {
    
    
            FileInputStream fileInputStream=new FileInputStream(file);
            while (fileInputStream.read()!=-1){
    
    
                System.out.println(fileInputStream.read());//这样直接输出 只会输出图片的字节码
            }
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }

    @Test
    public  void  test1(){
    
    
        File file=new File("0f61e4185b4ff57c170aaf96aa1a11e.jpg");
        File file1=new File("123.jpg");

        FileInputStream inputStream=null;
        FileOutputStream outputStream=null;
        try {
    
    


            inputStream=new FileInputStream(file);
            outputStream=new FileOutputStream(file1);

           byte[]bytes=new byte[5]; //此处注意byte为小写

            int len;
            while ((len=inputStream.read(bytes))!=-1){
    
    
                outputStream.write(bytes,0,len);
            }
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            try {
    
    
                inputStream.close();
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }

            try {
    
    
                outputStream.close();
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        }
    }
}


输出样式

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46351306/article/details/113797679