Package Org.example.io;
Import Java.io.ByteArrayInputStream;
Import java.io.IOException;
/**
* Bytearrayinputstream: Pass in the array when creating the object, do not need to pass the file, there is no new method, close () Close invalid
* The stream itself is an in-memory resource, and the content in the stream is also a resource in memory, so the memory is freed without manual shutdown, so there is no need to close the stream
* The content of the stream is a resource in memory and does not access the disk
*/
public class Testbytearrayinputstream {
public static void Main (string[] args) {
String s = "Hello world!";
Byte[] B = s.getbytes (); Gets the byte array of the string
Read (b);
}
private static void Read (byte[] b) {
Bytearrayinputstream Bais = new Bytearrayinputstream (b); Bytearrayinputstream the reason for not throwing exceptions when instantiating is that there is no relationship with the outside world
int i = 0;
Byte[] B1 = new byte[1024]; byte array buffers
StringBuilder sb = new StringBuilder (); StringBuilder is located in the Java.lang package, similar to a string
try {
while ((i = Bais.read (B1))! =-1) {
Sb.append (New String (B1, 0, i)); Attaches the specified string loop to SB
}
System.out.println (SB); Output SB-string
} catch (IOException e) {
E.printstacktrace ();
}
}
}
Memory stream (byte array stream) Bytearrayinputstream