zoukankan      html  css  js  c++  java
  • java IO之DataInputStream和DataOutputStream

     1 /**
     2  * 
     3  */
     4 package com.io.test;
     5 
     6 import java.io.ByteArrayInputStream;
     7 import java.io.ByteArrayOutputStream;
     8 import java.io.DataInputStream;
     9 import java.io.DataOutputStream;
    10 import java.io.IOException;
    11 
    12 import org.junit.Test;
    13 
    14 /**
    15  * utf-8比较省空间
    16  * @author chengjj
    17  * 
    18  */
    19 public class TestDataStream {
    20 
    21     @Test
    22     public void testDataStream() {
    23         try {
    24             ByteArrayOutputStream baos = new ByteArrayOutputStream();//内存中申请一块字节数组,具体多大随即的,不用去管
    25             DataOutputStream daos = new DataOutputStream(baos);
    26             daos.writeDouble(Math.random());//8个字节
    27             daos.writeBoolean(true);//1个字节
    28             
    29             
    30             ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    31             System.out.println(bais.available());//一共9个字节
    32             DataInputStream dais = new DataInputStream(bais);
    33             //先写进去的先读出来,一定要注意
    34             System.out.println(dais.readDouble());
    35             System.out.println(dais.readBoolean());
    36             
    37         } catch (IOException e) {
    38             e.printStackTrace();
    39         }
    40     }
    41 }
  • 相关阅读:
    LeetCode 230. Kth Smallest Element in a BST
    LeetCode 114. Flatten Binary Tree to Linked List
    LeetCode 222. Count Complete Tree Nodes
    LeetCode 129. Sum Root to Leaf Numbers
    LeetCode 113. Path Sum II
    LeetCode 257. Binary Tree Paths
    Java Convert String & Int
    Java Annotations
    LeetCode 236. Lowest Common Ancestor of a Binary Tree
    LeetCode 235. Lowest Common Ancestor of a Binary Search Tree
  • 原文地址:https://www.cnblogs.com/cjunj/p/2755731.html
Copyright © 2011-2022 走看看