ByteArrayOutputStream类是在创建它的实例时,程序内部创建一个byte型别数组的缓冲区,然后利用ByteArrayOutputStream和ByteArrayInputStream的实例向数组中写入或读出byte型数据。在网络传输中我们往往要传输很多变量,我们可以利用ByteArrayOutputStream把所有的变量收集到一起,然后一次性把数据发送出去。具体用法如下: 
ByteArrayOutputStream:    可以捕获内存缓冲区的数据,转换成字节数组

ByteArrayInputStream: 可以将字节数组转化为输入流

public static void main(String[] args) {
    int a = 0;
    int b = 1;
    int c = 2;
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    bout.write(a);
    bout.write(b);
    bout.write(c);
    byte[] buff = bout.toByteArray();
    for (int i = 0; i < buff.length; i++)
        System.out.println(buff[i]);
    System.out.println("***********************");
    ByteArrayInputStream bin = new ByteArrayInputStream(buff);
    while ((b = bin.read()) != -1) {
        System.out.println(b);
    }
}

如上所示,ByteArrayOutputStream把内存中的数据读到字节数组中,而ByteArrayInputStream又把字节数组中的字节以流的形式读出,实现了对同一个字节数组的操作.

综合DataOutputStream&DataInputStream的作用和功能,与ByteArrayOutputStream和ByteArrayInputSream使用将更方便.此时DataOutputStream&DataInputStream封闭了字节流,以适当的形式读出了字节数组中的数据.如下所示:

public static void main(String[] args) throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    DataOutputStream dout = new DataOutputStream(bout);
    String name = "xxy";
    int age = 84;
    dout.writeUTF(name);
    dout.writeInt(age);
    byte[] buff = bout.toByteArray();
    ByteArrayInputStream bin = new ByteArrayInputStream(buff);
    DataInputStream dis = new DataInputStream(bin);
    String newName = dis.readUTF();
    int newAge = dis.readInt();
    System.out.println(newName + ":" + newAge);
}

  

转载: http://blog.chinaunix.net/uid-9789791-id-1997411.html

ByteArrayOutputStream的用法的更多相关文章

  1. java学习总结之文件操作--ByteArrayOutputStream的用法

    ByteArrayOutputStream类是在创建它的实例时,程序内部创建一个byte型别数组的缓冲区, 然后利用ByteArrayOutputStream和ByteArrayInputStream ...

  2. [转载]java.ByteArrayInputStream与ByteArrayOutputStream再次理解

    一次看到ByteArrayOutputStream的时候是在Nutch的部分源码,后来在涉及IO操作时频频发现这两个类的踪迹,觉得确实是很好用,所以把它们的用法总结一下. ByteArrayOutpu ...

  3. 【转】ByteArrayOutputStream和ByteArrayInputStream详解

    ByteArrayOutputStream类是在创建它的实例时,程序内部创建一个byte型别数组的缓冲区,然后利用ByteArrayOutputStream和ByteArrayInputStream的 ...

  4. AsyncTask的用法总结

    这几天被AsyncTask虐得不行,在此总结下 首先: AsyncTask的参数介绍 在开发Android移动客户端的时候往往要使用多线程来进行操作,我们通常会将耗时的操作放在单独的线程执行,避免其占 ...

  5. java io系列03之 ByteArrayOutputStream的简介,源码分析和示例(包括OutputStream)

    前面学习ByteArrayInputStream,了解了“输入流”.接下来,我们学习与ByteArrayInputStream相对应的输出流,即ByteArrayOutputStream.本章,我们会 ...

  6. 关于HttpClient,HttpURLConnection,OkHttp的用法

    1 HttpClient入门实例 1.1发送get请求 /** * HttpClient发送get请求 * @param url 请求地址 * @return * @throws IOExceptio ...

  7. android Bitmap用法总结(转载)

    Bitmap用法总结1.Drawable → Bitmappublic static Bitmap drawableToBitmap(Drawable drawable) {Bitmap bitmap ...

  8. Java IO --ByteArrayOutputStream (六)***

    Java提供了很丰富的io接口,已经可以满足我们大部分读取数据的需求,这个在C读取数据需要自己定义缓冲区数组大小,而且要小心翼翼的防止缓冲区溢出的情况相当不同.一般情况下我们读取的数据都是直接读取成为 ...

  9. 转 关于HttpClient,HttpURLConnection,OkHttp的用法

    转自:https://www.cnblogs.com/zp-uestc/p/10371012.html 1 HttpClient入门实例 1.1发送get请求 1 2 3 4 5 6 7 8 9 10 ...

随机推荐

  1. codeforces Ilya and Matrix

    http://codeforces.com/contest/313/problem/C #include <cstdio> #include <cstring> #includ ...

  2. 【Xamarin For IOS 开发需要的安装文件】

    官网安装文件下载: http://download.xamarin.com/XamarinforMac/Mac/xamarin.mac-2.0.1.64.pkghttp://download.xama ...

  3. windows系统各版本 各种数据结构

    极爽啊http://msdn.moonsols.com/

  4. 关于ActionBar的向下兼容

    1. Create a blank Android Project创建一个空的Android项目.a. 导入ActionBarCompat工程.ActionBarCompat的source code位 ...

  5. [置顶] css3 befor after 简单使用 制作时尚焦点图相框

    :befor.:after是CSS的伪元素,什么是伪元素呢?伪元素用于向某些选择器设置特殊效果. 我们用CSS手册可以查询到其基本的用法: E:before/E::before 设置在对象前(依据对象 ...

  6. BaseAdapter自定义适配器——思路详解

    BaseAdapter自定义适配器——思路详解 引言: Adapter用来把数据绑定到扩展了AdapterView类的视图组.系统自带了几个原生的Adapter. 由于原生的Adapter视图功能太少 ...

  7. jquery图片3D旋绕效果 rotate3Di的操作

    这是一个图片效果,很简单实用,只需要一个"rotate3Di.js"的插件就行, 关于rotate的用法有如下几种: $(选择器).rotate3Di(30); //把图片3D旋转 ...

  8. java 字符串为空问题

    java 字符串为空问题 String testStr = null; System.out.println(testStr); if (testStr == null) { System.out.p ...

  9. 正则表达式获取URL参数

    使用到的正则表达式: [^\?&]?参数名=[^&]+ document.location.getURLPara = function (name) { var reg = new R ...

  10. Caused by: java.lang.ClassNotFoundException: javassist.ClassPool

    1.错误原因 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help ...