`
javacto
  • 浏览: 81944 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

DataIO 【012】

DOS 
阅读更多
package com.testdatastream;
import java.io.* ;
public class Test {

	/**
	 * @param args
	 * IO_ DataInputStream && DataOutputStream
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ByteArrayOutputStream baos = new ByteArrayOutputStream() ;
        DataOutputStream dos = new DataOutputStream(baos) ;
        try {
        	dos.writeDouble(Math.random()) ; //读入随机产生的一个数
        	dos.writeBoolean(true) ;
        	System.out.println(dos.size()) ;  //输出到目前为止写入此数据输出流的字节数
        	ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()) ; //toByteArray()创建一个新分配的 byte 数组
        	System.out.println(bais.available()) ; //返回可从此输入流读取(或跳过)的剩余字节数
        	DataInputStream dis = new DataInputStream(bais) ;
        	System.out.println(dis.readDouble()) ; //先写入的必须先读出,不能先读Boolean
        	System.out.println(dis.readBoolean()) ;
        	dos.close() ;
        	dis.close() ;
        } catch (IOException e) {
        	e.printStackTrace() ;
        }
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics