看這兩個類的名字就不難猜想出它們的類關係圖。java
DataOutputStream: 主要是一些writeXxx()操做,寫出, 至關於序列化ide
DataInputStream: 主要是一些readXxx()操做,寫入, 至關於反序列化this
通常狀況下,二者會成對出現。 在使用過程當中要特別注意, 一個writeXxx() 對應着一個readXxx(), 千萬不要用readInt()去對應writeBoolean().spa
說白了,就是序列化與反序列化要匹配。code
下面舉個例子,將Student對象的屬性序列化後保存到文件中,而後反序列還原,示例代碼以下:對象
public class Student { private int id; private String name; private long age; private boolean gender; public Student(int id, String name, long age, boolean gender) { this.id = id; this.name = name; this.age = age; this.gender = gender; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public long getAge() { return age; } public void setAge(long age) { this.age = age; } public boolean isGender() { return gender; } public void setGender(boolean gender) { this.gender = gender; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", gender=" + gender + '}'; } }
@Test public void testDataStream() throws IOException { // 建立一個Student對象 Student student = new Student(1, "鄭欽鋒", 100, true); // 建立一個DataOutputStream對象 OutputStream outputStream = new FileOutputStream("E:\\360downloads\\wpcache\\srvsetwp\\student.txt"); DataOutputStream dataOutputStream = new DataOutputStream(outputStream); // 對java的8種基本數據類型進行write操做,其實就至關於序列化操做 dataOutputStream.writeInt(student.getId()); dataOutputStream.writeUTF(student.getName()); dataOutputStream.writeLong(student.getAge()); dataOutputStream.writeBoolean(student.isGender()); // 關流 dataOutputStream.close(); // 讀取文件,進行反序列化操做 FileInputStream inputStream = new FileInputStream("E:\\360downloads\\wpcache\\srvsetwp\\student.txt"); DataInputStream dataInputStream = new DataInputStream(inputStream); int id = dataInputStream.readInt(); String name = dataInputStream.readUTF(); long age = dataInputStream.readLong(); boolean gender = dataInputStream.readBoolean(); // 還原這個student對象 Student retStudent = new Student(id, name, age, gender); System.out.println(retStudent); // Student{id=1, name='鄭欽鋒', age=100, gender=true} dataInputStream.close(); } }
若是序列化與反序列化不匹配,就是個悲劇,請看下面的示例:blog
@Test public void testDataStream() throws IOException { // 建立一個Student對象 Student student = new Student(1, "鄭欽鋒", 100, true); // 建立一個DataOutputStream對象 OutputStream outputStream = new FileOutputStream("E:\\360downloads\\wpcache\\srvsetwp\\student.txt"); DataOutputStream dataOutputStream = new DataOutputStream(outputStream); // 對java的8種基本數據類型進行write操做,其實就至關於序列化操做 dataOutputStream.writeLong(student.getAge()); dataOutputStream.writeBoolean(student.isGender()); dataOutputStream.writeUTF(student.getName()); dataOutputStream.writeInt(student.getId()); // 關流 dataOutputStream.close(); // 讀取文件,進行反序列化操做 FileInputStream inputStream = new FileInputStream("E:\\360downloads\\wpcache\\srvsetwp\\student.txt"); DataInputStream dataInputStream = new DataInputStream(inputStream); int id = dataInputStream.readInt(); String name = dataInputStream.readUTF(); long age = dataInputStream.readLong(); boolean gender = dataInputStream.readBoolean(); // 還原這個student對象 Student retStudent = new Student(id, name, age, gender); System.out.println(retStudent); // Student{id=0, name='', age=28148597348991889, gender=true} dataInputStream.close(); }
因此,write序列化的順序是: int(id) ---> string(name) ---> long(age)----> boolean(gender);get
read反序列化的順序必定要是: int(id) ---> string(name) ---> long(age)----> boolean(gender);input