IO流15 --- 数据流操作Java语言的基本数据类型 --- 技术搬运工(尚硅谷)

写入数据

@Test
public void test10() throws IOException {
    DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
    
    dos.writeUTF("猪八戒");
    dos.writeInt(26);
    dos.writeBoolean(true);
    
    dos.close();
}

读取数据

@Test
public void test11() throws IOException {
    DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
    
    String name = dis.readUTF();
    int age = dis.readInt();
    boolean isMale = dis.readBoolean();

    System.out.println("name = " + name);
    System.out.println("age = " + age);
    System.out.println("isMale = " + isMale);
    
    dis.close();
}
原文地址:https://www.cnblogs.com/noyouth/p/11751187.html