首页 > 编程语言 > 详细

Java官方操纵byte数组的方式

时间:2019-09-12 16:03:33      阅读:87      评论:0      收藏:0      [点我收藏+]

java官方提供了一种操作字节数组的方法——内存流(字节数组流)ByteArrayInputStream、ByteArrayOutputStream

ByteArrayOutputStream——byte数组合并

/**
    * 将所有的字节数组全部写入内存中,之后将其转化为字节数组
    */
    public static void main(String[] args) throws IOException {
        String str1 = "132";
        String str2 = "asd";
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        os.write(str1.getBytes());
        os.write(str2.getBytes());
        byte[] byteArray = os.toByteArray();
        System.out.println(new String(byteArray));
    }

 

ByteArrayInputStream——byte数组截取

/**
    *   从内存中读取字节数组
    */
    public static void main(String[] args) throws IOException {
        String str1 = "132asd";
        byte[] b = new byte[3];
        ByteArrayInputStream in = new ByteArrayInputStream(str1.getBytes());
        in.read(b);
        System.out.println(new String(b));
        in.read(b);
        System.out.println(new String(b));
    }

 

Java官方操纵byte数组的方式

原文:https://www.cnblogs.com/wangbin2188/p/11512192.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!