(五)IO流之ByteArrayInput/OutputStream

ByteArrayInputStream:是把字节数组当成源的输入流

    String string="hello shanghai";
    ByteArrayInputStream bis=new ByteArrayInputStream(string.getBytes());
    int data=-1;
    while ((data=bis.read())!=-1) {
        System.out.print((char)data);
        
    }
    //bis.close();无效

ByteArrayOutputStream:是把字节数组当做目标的输出流

ByteArrayOutputStream bos=new ByteArrayOutputStream();
     bos.write(97);
     bos.write("hello world".getBytes());
     byte[] buff=bos.toByteArray();
     for(byte data:buff) {
         System.out.println((char)data);
     }
     
     FileOutputStream fos=new FileOutputStream("D:\aa.txt",true);
     bos.writeTo(fos);//把 ByteArrayOutputStream内部缓冲区的数据写到对应的文件输出流中
     fos.close();
原文地址:https://www.cnblogs.com/tanlei-sxs/p/9665425.html