Java ByteArrayInputStream 类
java bytearrayinputstream 类
bytearrayinputstream 字节数组输入流在内存中创建一个字节数组缓冲区,从输入流读取的数据保存在该字节数组缓冲区中。
1. bytearrayinputstream 创建方式
创建字节数组输入流对象有以下几种方式。
接收字节数组作为参数创建:
bytearrayinputstream barray = new bytearrayinputstream(byte [] a);
另一种创建方式是接收一个字节数组,和两个整形变量 off、len,off表示第一个读取的字节,len表示读取字节的长度。
bytearrayinputstream barray = new bytearrayinputstream(byte []a, int off, int len)
2. bytearrayinputstream 操作方法
成功创建字节数组输入流对象后,可以参见以下列表中的方法,对流进行读操作或其他操作。
| 序号 | 方法描述 |
|---|---|
| 1 | public int read() 从此输入流中读取下一个数据字节。 |
| 2 | public int read(byte[] r, int off, int len) 将最多 len 个数据字节从此输入流读入字节数组。 |
| 3 | public int available() 返回可不发生阻塞地从此输入流读取的字节数。 |
| 4 | public void mark(int read) 设置流中的当前标记位置。 |
| 5 | public long skip(long n) 从此输入流中跳过 n 个输入字节。 |
3. bytearrayinputstream 范例
下面的例子演示了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 文件读写流
