ByteArrayInputStream and 文件转成字节数组再转回

package _03;


import java.io.*;
import java.nio.charset.StandardCharsets;

public class _3_21ByteArrayInputStream {
    public static void main(String[] args) {
//        FileInputStream是请求操作系统进行的
//        而ByteArray是虚拟机中的一块内存,GC管理,所以可以不需要关闭流
//        所有对象,包括数字都可以转成字节数组二进制,方便我们进行网络传送
//        字节数组不要过大
//        byte[] datas = " eveything will be ok".getBytes(StandardCharsets.UTF_8);
//        InputStream is = null;
//        try {
//            is = new ByteArrayInputStream(datas);
//            byte[] flush = new byte[5];
//            int len=-1;
//            while((len=is.read(flush))!=-1){
//               String str = new String(flush,0,flush.length);
//               System.out.println(str);
//            }
//        }catch (IOException e){
//            e.printStackTrace();
//        }//没有close方法

//        保存的位置,模拟网络节点或者服务器中的某个节点的存储
//        byte[] dest = null;
//        ByteArrayOutputStream baos = null;
//
//        baos = new ByteArrayOutputStream();
//        String msg = "nanjing  rain";
//        byte[] data1 = msg.getBytes(StandardCharsets.UTF_8);
//
//        baos.write(data1,0,data1.length);
//
//        //获取数据
//        dest = baos.toByteArray();
//        System.out.println(new String(dest));
//        try {
//            baos.flush();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

      byte[] datas = fileToByteArray("src\\01.jpg");
      System.out.println(datas.length);
    
    }
//    1.图片读取到字节数组
    public static byte[] fileToByteArray(String filename){

        File file = new File(filename);
        byte[] dest = null;

        InputStream ism = null;
        ByteArrayOutputStream baos = null;
        try {
            ism = new FileInputStream(file);
            baos = new ByteArrayOutputStream();
//            read的返回值int是字符的值
//            int temp;
//            while((temp = ism.read())!=-1){
//                System.out.println((char)(temp));
//            }
            byte[] flush = new byte[1024*10];  //容器大小
            int length = -1;//接收长度
            while((length=ism.read(flush))!=-1){
                baos.write(flush,0,length);
            }
            baos.flush();
            dest = baos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(ism!=null){
                    ism.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return dest;
    }
    // 2.字节数组到图片
    public static void byteArrayToFile(byte[] datas,String filename){

        File file = new File(filename);
        InputStream is = null;
        OutputStream osm = null;
        try{
            is = new ByteArrayInputStream(datas);
            osm = new FileOutputStream(file);
            byte[] flush = new byte[1024];
            int len=-1;
            while((len=is.read(flush))!=-1){
               osm.write(flush,0,len);
            }
            osm.flush();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                if(osm!=null){
                    osm.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值