package rxxj;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;//字节输入流的父类(抽象类)
public class Test{
public static void main(String []args){
byte[] bytes = new byte[]{1,2,3,4};
byte[] bytes2 = new byte[3];
InputStream inputStream = new ByteArrayInputStream(bytes);//字节数组输入流,用于读取字节数组中的数据
int x= 0;
try {
inputStream.read(bytes2,0,3);//将bytes中的数据读入到bytes2中
// while((x = inputStream.read()) != -1){//超出字节数组的范围,返回-1,即读完了
// System.err.println(x);
// }
for (int i = 0; i < bytes2.length; i++) {
System.err.println(bytes2[i]);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
ByteArrayInputStream
最新推荐文章于 2024-01-24 21:09:35 发布