Java ByteArrayInputStream.read()方法实例
ByteArrayInputStream
的Java ByteArrayInputStream.read()
方法的语法如下。
public int read()
示例
在下面的代码中展示了如何使用ByteArrayInputStream.read()
方法。
import java.io.ByteArrayInputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
byte[] buf = { 65, 66, 67, 68, 69 };
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
int b = 0;
while ((b = bais.read()) != -1) {
char c = (char) b;
System.out.println("byte :" + b + "; char : " + c);
}
System.out.print(bais.read() + " Reached the end");
}
}
执行上面的代码,得到如下结果 -
byte :65; char : A
byte :66; char : B
byte :67; char : C
byte :68; char : D
byte :69; char : E
-1 Reached the end