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 }
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 }