zoukankan      html  css  js  c++  java
  • Java官方操纵byte数组的方式

    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));
        }
  • 相关阅读:
    自动生成 Makefile 的全过程详解
    cpio实例讲解
    RPM命令手册
    SSHFS
    shell脚本命令行参数传递问题
    批量改名总结
    例解 autoconf 和 automake 生成 Makefile 文件
    Shell中的shift命令
    cpio命令详解
    Git笔记基础
  • 原文地址:https://www.cnblogs.com/wangbin2188/p/11512192.html
Copyright © 2011-2022 走看看