io中的字节数组流和将图片通过字节数组流进行转换

io中的字节数组输入流和字节数组输出流的区别 字节数组输入流: 1,创建源 是一个字节数组 2,选择流 ByteArrayInputStream() 3,操作 4,可以不用关闭 gc自动回收 不需要通过os 字节数组输出流 1,不需要创建源 2,选择流 ByteArrayOutputStream() 3,操作 需注意的是在内存中想要显示它,用toByteArray() 4,可以不用关闭 gc自动回收 不需要通过os

package cn.com.io;


import java.io.*;

public class TestIo07 {
          
   
    public static void main(String[] args) {
          
   
        //字节数组输入流
        //1 创建源 字节数组
        //2 选择流 ByteArrayInputStream()
        //3 操作
        //4 可以不用关闭
        byte[] bytes = "talk is cheap show me the code".getBytes();
        InputStream is = new ByteArrayInputStream(bytes);

        byte[] flush = new byte[3];
        int tmp;
        try {
          
   
            while((tmp = is.read(flush)) != -1) {
          
   
                String str = new String(flush,0,tmp);
                System.out.println(str);
            }
        }catch(IOException e) {
          
   
            e.printStackTrace();
        }

        //字节数组输出流
        //1 没有源
        //2 选择流 不需要传参
        //3 操作
        //4 可以不用关闭
        //获取数据 toByteArray()
        OutputStream os = new ByteArrayOutputStream();
        String str = "show me the code";
        byte[] flushs = str.getBytes();
        try {
          
   
            os.write(flushs,0,flushs.length);
            os.flush();
            byte[] flag = ((ByteArrayOutputStream) os).toByteArray();
            System.out.println(flag.length+"--->"+new String(flushs,0,((ByteArrayOutputStream) os).size()));
        }catch(IOException e) {
          
   
            e.printStackTrace();
        }

    }
}

综合运用(将图片转换成字节数组,再将其还原)

package cn.com.io;
import java.io.*;
public class TestIo08 {
          
   
    public static void main(String[] args) {
          
   
        //对接流(综合运用)
        //图片转成字节数组
        //1 图片到程序 2 程序到字节数组
        //字节数组转成图片
        //1 字节数组到程序 2 程序到图片
        byte[] bytes = fileToByteArray("kt.jpg");
        System.out.println(bytes.length);
        byteArrayToFile(bytes,"kt3.jpg");
    }
    private static byte[] fileToByteArray(String src) {
          
   
        File file = new File(src);
        InputStream is = null;
        OutputStream os = null;
        try {
          
   
            is = new FileInputStream(file);
            os = new ByteArrayOutputStream();
            byte[] bytes = new byte[1024*10];
            int tmp;
            while((tmp = is.read(bytes)) != -1) {
          
   
                os.write(bytes,0,tmp);
            }
            os.flush();
            return ((ByteArrayOutputStream) os).toByteArray();
        }catch(FileNotFoundException e) {
          
   
            e.printStackTrace();
        }catch(IOException e) {
          
   
            e.printStackTrace();
        }finally {
          
   
            try {
          
   
                if (is != null) {
          
   
                    is.close();
                }
            }catch(IOException e) {
          
   
                e.printStackTrace();
            }
        }
        return null;
    }
    private static void byteArrayToFile(byte[] src,String dest) {
          
   
        InputStream is = new ByteArrayInputStream(src);
        OutputStream os = null;
        try {
          
   
            os = new FileOutputStream(dest);
            byte[] bytes = new byte[1024*10];
            int tmp;
            while((tmp = is.read(bytes)) != -1) {
          
   
                os.write(bytes,0,tmp);
            }
            os.flush();
        }catch(FileNotFoundException e) {
          
   
            e.printStackTrace();
        } catch(IOException e) {
          
   
            e.printStackTrace();
        }finally {
          
   
            try {
          
   
                if (os != null) {
          
   
                    os.close();
                }
            }catch(IOException e) {
          
   
                e.printStackTrace();
            }
        }
    }
}
经验分享 程序员 微信小程序 职场和发展