问题:如何把一个long类型的数写进一个文件里

所以现在有DataInputStream和DataOutputStream

这两个是节点流

例子代码:

  1. import java.io.*;
  2.  
  3. public class TestDataStream {
  4. public static void main(String[] args) {
  5. ByteArrayOutputStream baos = new ByteArrayOutputStream();//这句话里执行了两个操作,首先在内存里分配了个字节数组。然后这里你看有一个OutputStream所以有一个管道,这个管道已经插在了内存里的这个字节数组上了。
  6. DataOutputStream dos = new DataOutputStream(baos);//dos是个管道套在上面那个ByteArrayOutputStream上,通过dos可以直接把一个数写进去,比如double型的字节数组里面直接就占了八个字节,而不用再转成字符串再写进去。
  7. try {
  8. dos.writeDouble(Math.random());
  9. dos.writeBoolean(true);//这步结束后字符数组有九个字节了
  10. ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());//baos是个管,连接字节数组的管,通过这个方法,我们就可以拿到这个管关于字节数组的引用
  11. System.out.println(bais.available());//这个方法是说 这个bais里面有多少个字节可以供我们读
  12. DataInputStream dis = new DataInputStream(bais);//本来通过bais往外读只能一个字节一个字节地往外读,那都出来谁知道是什么。所以现在套一个DataInputStream,就可以往外读double啊boolean什么的
  13. System.out.println(dis.readDouble());//一定注意!!先写的先读,你先写进去一个double,八个字节,那么先读double,直接读八个字节的double出来。
  14. System.out.println(dis.readBoolean());//实际上是读一个字节然后内部进行转换把它转换成boolean类型。
  15. dos.close();
  16. dis.close();
  17. } catch(IOException e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. }

数据流和ByteArray的更多相关文章

  1. ByteArrary(优化数据存储和数据流)

    原地址:http://www.unity蛮牛.com/blog-1801-799.html 首页 博客 相册 主题 留言板 个人资料   ByteArrary(优化数据存储和数据流) 分类:unity ...

  2. 【python】bytearray和string之间转换,用在需要处理二进制文件和数据流上

    最近在用python搞串口工具,串口的数据流基本读写都要靠bytearray,而我们从pyqt的串口得到的数据都是string格式,那么我们就必须考虑到如何对这两种数据进行转换了,才能正确的对数据收发 ...

  3. Java:IO流其他类(字节数组流、字符数组流、数据流、打印流、Properities、对象流、管道流、随机访问、序列流、字符串读写流)

    一.字节数组流: 类 ByteArrayInputStream:在构造函数的时候,需要接受数据源,而且数据源是一个字节数组. 包含一个内部缓冲区,该缓冲区包含从流中读取的字节.内部计数器跟踪 read ...

  4. flash从数据流判断图片格式防止xss攻击

    前段时间测试人员报了一个flash的xss bug,经分析用了Loader.loadBytes且没做数据流格式校验的程序都会中招,自测方法只需一行代码: ExternalInterface.call( ...

  5. angular2系列教程(九)Jsonp、URLSearchParams、中断选择数据流

    大家好,今天我们要讲的是http模块的第二部分,主要学习ng2中Jsonp.URLSearchParams.observable中断选择数据流的用法. 例子

  6. Android Bitmap 和 ByteArray的互相转换

    Android Bitmap 和 ByteArray的互相转换 移动平台图像处理,需要将图像传给native处理,如何传递?将bitmap转换成一个 byte[] 方便传递也方便cpp代码直接处理图像 ...

  7. 《连载 | 物联网框架ServerSuperIO教程》- 10.持续传输大块数据流的两种方式(如:文件)

    1.C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 <连载 | 物联网框架ServerSuperIO教程>1.4种通讯模式机制. <连载 | 物联网框架Serve ...

  8. [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  9. [LeetCode] Find Median from Data Stream 找出数据流的中位数

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

随机推荐

  1. win8 metro 自己写摄像头录像项目

    这是要求不适用CameraCaptureUI等使用系统自带的 camera  UI界面.要求我们自己写调用摄像头摄像的方法,如今我把我的程序贴下: UI界面的程序: <Page x:Class= ...

  2. 【java项目实战】一步步教你使用MyEclipse搭建java Web项目开发环境(一)

    首先.在開始搭建MyEclipse的开发环境之前.还有三步工具的安装须要完毕,仅仅要在安装配置成功之后才干够进入以下的java Web项目开发环境的搭建. 1.安装工具 第一步,下载并安装JDK,到官 ...

  3. 我们工作到底为了什么 (HP大中华区总裁孙振耀退休感言)

    我们工作到底为了什么 (HP大中华区总裁孙振耀退休感言) 一.关于工作与生活    我有个有趣的观察,外企公司多的是25-35岁的白领,40岁以上的员工很少,二三十岁的外企员工是意气风发的,但外企公司 ...

  4. 关于OutOfMemoryError的处理

    转自:http://www.blogjava.net/rosen/archive/2010/05/21/321575.html http://www.blogjava.net/rosen/archiv ...

  5. java8--IO工具类(java疯狂讲义3复习笔记)

    Paths类 public static void pathTest(){ Path path = Paths.get("~"); System.out.println(path) ...

  6. How can I pass data from Flask to JavaScript in a template?

    https://stackoverflow.com/questions/11178426/how-can-i-pass-data-from-flask-to-javascript-in-a-templ ...

  7. BootstrapValidator demo

    source:http://bv.doc.javake.cn/api/ BootstrapValidator is the best jQuery plugin to validate form fi ...

  8. leetcode 660. Remove 9

    Start from integer 1, remove any integer that contains 9 such as 9, 19, 29... So now, you will have ...

  9. HDU1698 Just a Hook —— 线段树 区间染色

    题目链接:https://vjudge.net/problem/HDU-1698 In the game of DotA, Pudge’s meat hook is actually the most ...

  10. YTU 1003: Redraiment的遭遇

    1003: Redraiment的遭遇 时间限制: 1000 Sec  内存限制: 128 MB 提交: 198  解决: 71 题目描述 Redraiment的老家住在工业区,日耗电量非常大.是政府 ...