public class IOTest10 {
public static void main(String[] args) throws IOException {
//将指定字符串存放到byte数组中
byte[] bytes="Talk is cheap,show me the code".getBytes();//创建源
InputStream is=null;//选择流
is=new ByteArrayInputStream(bytes);
byte[] bytes1=new byte[5];//每次读取5个字节
int len=-1;
while((len=is.read(bytes1))!=-1){
String string=new String(bytes1,0,len);//转换成字符串
System.out.println(string);
}
}
}
public class IOTest11 {
public static void main(String[] args) {
byte[] dest=null;//创建源
ByteArrayOutputStream os=null;//选择流
os=new ByteArrayOutputStream();//不需要参数
String string="Talk is cheap,show me the code";
byte[] bytes=string.getBytes();
try {
os.write(bytes,0,bytes.length);//输出
os.flush();
dest=os.toByteArray(); //获取数据
System.out.println(bytes.length+"-->"+new String(dest,0,os.size()));//dest.length()
} catch (IOException e) {
e.printStackTrace();
}
}
}
来源:CSDN
作者:Reverse train.
链接:https://blog.csdn.net/xylyaya/article/details/104222200