import java.io.*;
import java.util.*;public class Test1 implements Serializable {
private static List<Person> list = new ArrayList<Person>();
public static void main(String[] args){
readFromFile();
}

public static void readFromFile(){
InputStream is = null;
ObjectInputStream ois = null;
try {
is = new FileInputStream("d:\\1.data");
ois = new ObjectInputStream(is);
Person p = null;
while((p = (Person)ois.readObject()) != null){
list.add(p);
System.out.println(p.getName());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}import java.io.Serializable;public class Person implements Serializable{
private static final long serialVersionUID = 1l;
private String name;
private int age;
Person(String name, int age){
this.name = name;
this.age = age;
}
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;
}

}

解决方案 »

  1.   

    你试试前两段代码,看能否成功,如果不成功,你得检查那个Person类是否编写正确,他必须遵循好几个规范[code=Java][/////////////////////////////////////////////////////////////////////////////////////////////////////////private static <T> void writeObjectToFile(T t, String fileName) {// 将一个对象 写入文件
    if (t == null)
        return;// 空对象默认不写进去
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(new FileOutputStream(fileName));
        oos.writeObject(t);
        oos.flush();
        oos.writeObject(null);// //写入结束标志方便读取(如果不写入,在读取的时候无法定位读取结束);
        oos.flush();
    } catch (Exception e) {
    } finally {
        try {
    if (oos != null)
        oos.close();
        } catch (Exception e) {
        }
    }
        }    private static <T> T readObjectFromFile(String fileName) {// 从文件中读出一个对象
    ObjectInputStream ois = null;
    try {
        ois = new ObjectInputStream(new FileInputStream(fileName));
        T t = (T) ois.readObject();
        return t; } catch (Exception e) {
    } finally {
        try {
    if (ois != null)
        ois.close();
        } catch (Exception e) {
        }
    }
    return null;
        }
    /////////////////////////////////////////////////////////////////////////////////////////////////////
     private static <T> void writeToFile(Collection<T> collection,
        String fileName) {// 将集合中的对象 写入文件
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(new FileOutputStream(fileName));
        Iterator<T> iterator = collection.iterator();
        while (iterator.hasNext()) {
    T t = (T) iterator.next();
    oos.writeObject(t);
    oos.flush();
        }
        oos.writeObject(null);// //写入结束标志方便读取(如果不写入,在读取的时候无法定位读取结束);
        oos.flush();
    } catch (Exception e) {
    } finally {
        try {
    if (oos != null)
        oos.close();
        } catch (Exception e) {
        }
    }
        }    private static <T> Collection readFromFile(String fileName) {// 从文件中读取对象
    // 返回一个集合
    ObjectInputStream ois = null;
    Collection<T> collection = new ArrayList<T>();
    try {
        ois = new ObjectInputStream(new FileInputStream(fileName));
        Object tempObject = null;
        T t = null;
        while ((tempObject = ois.readObject()) != null) {
    t = (T) tempObject;
    collection.add(t);
        }
        return collection;
    } catch (Exception e) {
    } finally {
        try {
    if (ois != null)
        ois.close();
        } catch (Exception e) {
        }
    }
    return null;
        }/code]
      

  2.   

    你试试前两段代码,看能否成功,如果不成功,你得检查那个Person类是否编写正确,他必须遵循好几个规范/////////////////////////////////////////////////////////////////////////////////////////////////////////private static <T> void writeObjectToFile(T t, String fileName) {// 将一个对象 写入文件
    if (t == null)
    return;// 空对象默认不写进去
    ObjectOutputStream oos = null;
    try {
    oos = new ObjectOutputStream(new FileOutputStream(fileName));
    oos.writeObject(t);
    oos.flush();
    oos.writeObject(null);// //写入结束标志方便读取(如果不写入,在读取的时候无法定位读取结束);
    oos.flush();
    } catch (Exception e) {
    } finally {
    try {
    if (oos != null)
    oos.close();
    } catch (Exception e) {
    }
    }
      }  private static <T> T readObjectFromFile(String fileName) {// 从文件中读出一个对象
    ObjectInputStream ois = null;
    try {
    ois = new ObjectInputStream(new FileInputStream(fileName));
    T t = (T) ois.readObject();
    return t;} catch (Exception e) {
    } finally {
    try {
    if (ois != null)
    ois.close();
    } catch (Exception e) {
    }
    }
    return null;
      }
    /////////////////////////////////////////////////////////////////////////////////////////////////////
     private static <T> void writeToFile(Collection<T> collection,
    String fileName) {// 将集合中的对象 写入文件
    ObjectOutputStream oos = null;
    try {
    oos = new ObjectOutputStream(new FileOutputStream(fileName));
    Iterator<T> iterator = collection.iterator();
    while (iterator.hasNext()) {
    T t = (T) iterator.next();
    oos.writeObject(t);
    oos.flush();
    }
    oos.writeObject(null);// //写入结束标志方便读取(如果不写入,在读取的时候无法定位读取结束);
    oos.flush();
    } catch (Exception e) {
    } finally {
    try {
    if (oos != null)
    oos.close();
    } catch (Exception e) {
    }
    }
      }  private static <T> Collection readFromFile(String fileName) {// 从文件中读取对象
    // 返回一个集合
    ObjectInputStream ois = null;
    Collection<T> collection = new ArrayList<T>();
    try {
    ois = new ObjectInputStream(new FileInputStream(fileName));
    Object tempObject = null;
    T t = null;
    while ((tempObject = ois.readObject()) != null) {
    t = (T) tempObject;
    collection.add(t);
    }
    return collection;
    } catch (Exception e) {
    } finally {
    try {
    if (ois != null)
    ois.close();
    } catch (Exception e) {
    }
    }
    return null;
      }
      

  3.   

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Iterator;public class TestPerson {
        static void test() {
        }    public static void main(String[] args) {
    writeObjectToFile(new Person("张三", 20), "c:\\person.data");
    Person p = readObjectFromFile("c:\\person.data");
    System.out.println(p);


    ArrayList<Person> persons = new ArrayList<Person>();
    persons.add(new Person("李四", 21));
    persons.add(new Person("王五", 22));

    writeToFile(persons,"c:/persons");
    ArrayList<Person> temppersons  = (ArrayList<Person>) readFromFile("c:/persons");
    for(int i=0; i<temppersons.size(); i++){
        System.out.println(temppersons.get(i));
    }
        



        }    /////////////////////////////////////////////////////////////////////////////////////////////////////////    private static <T> void writeObjectToFile(T t, String fileName) {// 将一个对象 写入文件
    if (t == null)
        return;// 空对象默认不写进去
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(new FileOutputStream(fileName));
        oos.writeObject(t);
        oos.flush();
        oos.writeObject(null);// //写入结束标志方便读取(如果不写入,在读取的时候无法定位读取结束);
        oos.flush();
    } catch (Exception e) {
    } finally {
        try {
    if (oos != null)
        oos.close();
        } catch (Exception e) {
        }
    }
        }    private static <T> T readObjectFromFile(String fileName) {// 从文件中读出一个对象
    ObjectInputStream ois = null;
    try {
        ois = new ObjectInputStream(new FileInputStream(fileName));
        T t = (T) ois.readObject();
        return t; } catch (Exception e) {
    } finally {
        try {
    if (ois != null)
        ois.close();
        } catch (Exception e) {
        }
    }
    return null;
        }    /////////////////////////////////////////////////////////////////////////////////////////////////////
        private static <T> void writeToFile(Collection<T> collection,
        String fileName) {// 将集合中的对象 写入文件
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(new FileOutputStream(fileName));
        Iterator<T> iterator = collection.iterator();
        while (iterator.hasNext()) {
    T t = (T) iterator.next();
    oos.writeObject(t);
    oos.flush();
        }
        oos.writeObject(null);// //写入结束标志方便读取(如果不写入,在读取的时候无法定位读取结束);
        oos.flush();
    } catch (Exception e) {
    } finally {
        try {
    if (oos != null)
        oos.close();
        } catch (Exception e) {
        }
    }
        }    private static <T> Collection readFromFile(String fileName) {// 从文件中读取对象
    // 返回一个集合
    ObjectInputStream ois = null;
    Collection<T> collection = new ArrayList<T>();
    try {
        ois = new ObjectInputStream(new FileInputStream(fileName));
        Object tempObject = null;
        T t = null;
        while ((tempObject = ois.readObject()) != null) {
    t = (T) tempObject;
    collection.add(t);
        }
        return collection;
    } catch (Exception e) {
    } finally {
        try {
    if (ois != null)
        ois.close();
        } catch (Exception e) {
        }
    }
    return null;
        }}class Person implements Serializable {
        private static final long serialVersionUID = 6213933727640755594L;
        private String name;
        private int id;    Person(String n, int i) {
    name = n;
    id = i;
        }    public String getName() {
    return name;
        }    public int getId() {
    return id;
        }    public String toString() {
    return "name:" + name + "  id:" + id;
        }
    }/*output:
    name:张三  id:20
    name:李四  id:21
    name:王五  id:22
     */
      

  4.   

    楼上的是个好办法,一般没法判断是否读取到文件的末尾了
    要么将所有的对象封装到一个ArrayList里面再写入到文件中;要么就在写的时候最末写入一个null,读取的时候判断是否为null就可以了