java DataInputStream和DataOutputStream详解及实例代码

时间:2022-09-07 20:24:48

java DataInputStream和DataOutputStream详解

操作基本数据类型的流 

DataInputStream

DataOutputStream

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class IntegerDemo {
  public static void main(String[] args) throws IOException {
    myWrite();
 
    myReader();
  }
 
  private static void myWrite() throws IOException {
    // TODO Auto-generated method stub
    // 创建数据输出流对象
    FileOutputStream fos = new FileOutputStream("E:\\zikao\\file\\cs.txt");
    DataOutputStream dos = new DataOutputStream(fos);
 
    // 写数据
    dos.writeByte(10);
    dos.writeShort(100);
    dos.writeInt(1000);
    dos.writeLong(10000);
    dos.writeFloat(12.34F);
    dos.writeDouble(12.56);
    dos.writeChar('a');
    dos.writeBoolean(true);
 
    // 释放资源
    dos.close();
  }
 
  private static void myReader() throws IOException {
    // TODO Auto-generated method stub
    // 创建数据输入流对象
    FileInputStream fis = new FileInputStream("E:\\zikao\\file\\cs.txt");
    DataInputStream dis = new DataInputStream(fis);
 
    // 读数据
    byte b = dis.readByte();
    short s = dis.readShort();
    int i = dis.readInt();
    long l = dis.readLong();
    float f = dis.readFloat();
    double d = dis.readDouble();
    char c = dis.readChar();
    boolean bl = dis.readBoolean();
 
    // 释放资源
    dis.close();
 
    System.out.println(b);
    System.out.println(s);
    System.out.println(i);
    System.out.println(l);
    System.out.println(f);
    System.out.println(d);
    System.out.println(c);
    System.out.println(bl);
  }
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!