Java中的ByteArrayInputStream类
AI 概述
字节数组输入流在内存中创建一个字节数组缓冲区,从输入流读取的数据保存在该字节数组缓冲区中。创建字节数组输入流对象有以下几种方式。
接收字节数组作为参数创建:
ByteArrayInputStream bArray = new ByteArrayInputStream(byte [] a);
另一种创建方式是接收一个字节数组,和两个整形变量 off、len,off 表示第一个...
字节数组输入流在内存中创建一个字节数组缓冲区,从输入流读取的数据保存在该字节数组缓冲区中。创建字节数组输入流对象有以下几种方式。
接收字节数组作为参数创建:
ByteArrayInputStream bArray = new ByteArrayInputStream(byte [] a);
另一种创建方式是接收一个字节数组,和两个整形变量 off、len,off 表示第一个读取的字节,len 表示读取字节的长度。
ByteArrayInputStream bArray = new ByteArrayInputStream(byte []a,
int off,
int len)
成功创建字节数组输入流对象后,可以参见以下列表中的方法,对流进行读操作或其他操作。
| 方法 | 描述 |
|---|---|
| public int read() | 从此输入流中读取下一个数据字节。 |
| public int read(byte[] r, int off, int len) | 将最多 len 个数据字节从此输入流读入字节数组。 |
| public int available() | 返回可不发生阻塞地从此输入流读取的字节数。 |
| public void mark(int read) | 设置流中的当前标记位置。 |
| public long skip(long n) | 从此输入流中跳过 n 个输入字节。 |
实例
下面的例子演示了ByteArrayInputStream 和 ByteArrayOutputStream的使用:
import java.io.*;
public class ByteStreamTest {
public static void main(String args[])throws IOException {
ByteArrayOutputStream bOutput = new ByteArrayOutputStream(12);
while( bOutput.size()!= 10 ) {
// 获取用户输入值
bOutput.write(System.in.read());
}
byte b [] = bOutput.toByteArray();
System.out.println("Print the content");
for(int x= 0 ; x < b.length; x++) {
// 打印字符
System.out.print((char)b[x] + " ");
}
System.out.println(" ");
int c;
ByteArrayInputStream bInput = new ByteArrayInputStream(b);
System.out.println("Converting characters to Upper case " );
for(int y = 0 ; y < 1; y++ ) {
while(( c= bInput.read())!= -1) {
System.out.println(Character.toUpperCase((char)c));
}
bInput.reset();
}
}
}
以上实例编译运行结果如下:
asdfghjkly Print the content a s d f g h j k l y Converting characters to Upper case A S D F G H J K L Y
以上关于Java中的ByteArrayInputStream类的文章就介绍到这了,更多相关内容请搜索码云笔记以前的文章或继续浏览下面的相关文章,希望大家以后多多支持码云笔记。
声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 admin@mybj123.com 进行投诉反馈,一经查实,立即处理!
重要:如软件存在付费、会员、充值等,均属软件开发者或所属公司行为,与本站无关,网友需自行判断
码云笔记 » Java中的ByteArrayInputStream类
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 admin@mybj123.com 进行投诉反馈,一经查实,立即处理!
重要:如软件存在付费、会员、充值等,均属软件开发者或所属公司行为,与本站无关,网友需自行判断
码云笔记 » Java中的ByteArrayInputStream类

微信
支付宝