ByteArrayInputStream类允许将内存中的缓冲区用作InputStream,输入源是一个字节数组。
ByteArrayInputStream类提供以下构造函数。
Sr.No | 构造函数和说明 |
---|---|
1 | ByteArrayInputStream(byte [] a) 此构造函数接受字节数组作为参数。 |
2 | ByteArrayInputStream(byte [] a,int off,int len) 此构造函数采用字节数组和两个整数值,其中 off 是要读取的第一个字节,而 len 是要读取的字节数。 |
Sr.No. | Method & Remark |
---|---|
1 | public int read() 此方法从InputStream读取下一个数据字节。 |
2 | public int read(byte [] r,int off,int len) 该方法从InputStream中的 off 开始最多读取 len 个字节。 |
3 | public int available() 给出可以从此文件输入流读取的字节数。 链接:https://www.learnfk.comhttps://www.learnfk.com/java/java-bytearrayinputstream.html 来源:LearnFk无涯教程网 |
4 | public void mark(int read) 这将设置流中的当前标签位置。 |
5 | public long skip(long n) 从流中跳过" n"个字节。这将返回跳过的实际字节数。 |
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("hello".getBytes()); } 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(); } } }
以下是上述程序的示例运行-
Print the content h e l l o h e l l o Converting characters to Upper case H E L L O H E L L O
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)