package test; import java.io.*; import java.sql.SQLClientInfoException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.CountDownLatch; import java.util.zip.InflaterInputStream; import javax.annotation.processing.FilerException; import javax.management.RuntimeErrorException; import privateclass.Filterby_Name; import privateclass.Filterby_hidden; import privateclass.Filterby_java; import privateclass.MyBufferedReader; import privateclass.Person; public class Main { private static final String space_operator = " "; private static final double pi = Math.PI; private static final String LINE_SEPARATOR = System.getProperty("line.separator"); private static final int SIZE = 1024*1024; public static void main(String[] args) throws Exception { writeobj(); readobject(); } /** * 将对象写入硬盘的时候我们需要将obj序列化 * 也就是在需要写入对象的类里面实现Serializable接口(标记接口什么都不需要覆盖) * 我们可以给类标记一个serialVersionUID * @throws IOException * @throws FileNotFoundException * @throws ClassNotFoundException */ public static void readobject() throws IOException, FileNotFoundException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.object")); Person person = (Person) ois.readObject(); System.out.println(person.getNaem() + " " + person.getAge()); } /** * 我们在读取object的时候需要反序列化 * 我们一定要有那个创建对象的类文件才能读入对象 * @throws IOException * @throws FileNotFoundException */ public static void writeobj() throws IOException, FileNotFoundException { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.object")); oos.writeObject(new Person("张三", 15)); oos.close(); } } package privateclass; import java.io.Serializable; public class Person implements Comparable<Object>,Serializable{ /** * 当我们写入对象的时候如果不想写入他的某一项内容我们可以加上static或者加上transient */ private static final long serialVersionUID = 1L; private String naem; private int age; public String getNaem() { return naem; } public void setNaem(String naem) { this.naem = naem; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Person(String naem, int age) { super(); this.naem = naem; this.age = age; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((naem == null) ? 0 : naem.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (age != other.age) return false; if (naem == null) { if (other.naem != null) return false; } else if (!naem.equals(other.naem)) return false; return true; } public Person() { super(); } @Override public String toString() { return "Person [naem=" + naem + ", age=" + age + "]"; } @Override public int compareTo(Object o) { Person p = (Person) o; int temp = this.age - p.age; return temp == 0 ? this.naem.compareTo(p.naem) : temp; } }