Java byte数组操纵方式代码实例解析
作者:Mars.wang 时间:2022-02-18 16:54:12
字节数组的关键在于它为存储在该部分内存中的每个8位值提供索引(快速),精确的原始访问,并且您可以对这些字节进行操作以控制每个位。 坏处是计算机只将每个条目视为一个独立的8位数 - 这可能是你的程序正在处理的,或者你可能更喜欢一些强大的数据类型,如跟踪自己的长度和增长的字符串 根据需要,或者一个浮点数,让你存储说3.14而不考虑按位表示。 作为数据类型,在长数组的开头附近插入或移除数据是低效的,因为需要对所有后续元素进行混洗以填充或填充创建/需要的间隙。
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));
}
来源:https://www.cnblogs.com/wangbin2188/p/11512192.html
标签:Java,byte,数组
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于Java实现简单贪吃蛇游戏
2022-08-07 02:09:44
![](https://img.aspxhome.com/file/2023/8/63488_0s.jpg)
详解Android中Handler的内部实现原理
2023-05-11 12:50:12
![](https://img.aspxhome.com/file/2023/0/101860_0s.png)
Java中自动装箱、拆箱引起的耗时详解
2023-01-11 11:42:42
详解Java编程中线程同步以及定时启动线程的方法
2021-08-31 10:59:20
Mybatis实现分表插件
2023-07-23 12:41:54
![](https://img.aspxhome.com/file/2023/2/80292_0s.gif)
IDEA类与方法注释模板设置图文教程(非常详细)
2022-02-24 10:18:29
![](https://img.aspxhome.com/file/2023/9/94189_0s.png)
解决Android ListView数据为空及加载错误的方法
2022-11-30 06:57:41
![](https://img.aspxhome.com/file/2023/1/91011_0s.png)
Mybatis控制台打印SQL语句的两种方式实现
2023-06-23 14:24:57
如何用C#在PC上查找连接蓝牙设备并实现数据传输
2021-11-04 07:54:42
Java8 Comparator: 列表排序的深入讲解
2022-12-11 08:30:01
Springboot+Mybatis-plus不使用SQL语句进行多表添加操作及问题小结
2021-09-30 10:31:10
![](https://img.aspxhome.com/file/2023/7/94377_0s.png)
Android Studio gradle 编译提示‘default not found’ 解决办法
2023-07-19 09:23:06
WPF MVVM示例讲解
2022-04-16 22:47:20
![](https://img.aspxhome.com/file/2023/7/102257_0s.png)
SpringMVC 限流的示例代码
2022-08-21 09:48:51
Java实现登录与注册页面
2023-04-17 03:21:20
![](https://img.aspxhome.com/file/2023/1/61001_0s.jpg)
Android自定义组件获取本地图片和相机拍照图片
2022-07-09 21:54:48
![](https://img.aspxhome.com/file/2023/1/101831_0s.gif)
Java如何实现通过证书访问Https请求
2021-10-19 08:51:36
ResultSet如何动态获取列名和值
2022-01-16 15:54:01
Android调用google地图生成路线图实现代码
2023-06-04 09:37:25
Spring@Value使用获取配置信息为null的操作
2021-08-01 23:46:27