package com.IO.kkk; import java.io.Serializable; import java.util.Objects; public class Person implements Serializable { private static final long serialVersionUID = 20050210l; private float id; private String name; private int age; public float getId() { return id; } public void setId(float id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Person() { } public Person(float id, String name, int age) { this.id = id; this.name = name; this.age = age; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; return id == person.id && Objects.equals(name, person.name); } @Override public int hashCode() { return Objects.hash(id,name); } @Override public String toString() { return "xinobi{" + "id=" + id + "name=" + name + "age=" + age; } }
package com.IO.kkk; import java.io.*; public class Ios { public static void main(String[] args) { Person obito = new Person(010886f,"宇智波带土",29); FileOutputStream fis = null; ObjectOutputStream ois = null; try { fis = new FileOutputStream("D://obito"); ois = new ObjectOutputStream(fis); ois.writeObject(obito); ois.flush(); } catch (IOException e) { e.printStackTrace(); }finally { if (ois != null){ try { ois.close(); } catch (IOException e) { throw new RuntimeException(e); } } if (fis != null){ try { fis.close(); } catch (IOException e) { throw new RuntimeException(e); } } } } }