字节数组流

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();
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值