1、字节数组输入流
package IOTest_4;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
public class ByteArrayInputStreamTest_1 {
public static void main(String[] args) {
byte[] src = "abcdefg".getBytes();
InputStream is = null;
is = new ByteArrayInputStream(src);
byte[] b = new byte[5];
int len = -1;
try {
while((len=is.read(b))!=-1) {
String str = new String(b,0,len);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
字节数组输出流
package IOTest_4;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class ByteArrayOutputStreamTest_1 {
public static void main(String[] args) {
byte[] data = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String str = "abcdefghijklmnopqrstuvwxyz";
byte[] b = str.getBytes();
try {
baos.write(b,0,b.length);
baos.flush();
data = baos.toByteArray();
System.out.println(new String(data,0,data.length));
} catch (IOException e) {
e.printStackTrace();
}
}
}