解决方案 »

  1.   

    产生1500个不同的对象,存入文件,然后再从文件中读取恢复这些对象出来。
    demo只是很个很烂的演示,期待高手的建议。
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;class Person implements Serializable{
        private static final long serialVersionUID = -1725073815168650916L;
        private int id;
        private String name;
        
        public Person(int id, String name){
            this.id = id;
            this.name = name;
        }
        
        @Override
        public String toString() {
            return String.format("id=[%d], name=[%s]%n", this.id, this.name);
        }
    }
    public class Demo {      /**
         * 将Object对象存入数组,接着存入文件
         * 
         * Object可以为任意对象,本demo为Person
         * 
         * @param objects   需要存储的对象数组
         * @param fileName  存储文件名
         * 
         * @throws FileNotFoundException
         * @throws IOException
         */
        public static void saveObject(Object[] objects, String fileName) throws FileNotFoundException, IOException{
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileName, true));
            // 在文件头写1个int,表示存储了多少个对象
            int numOfObject = objects.length;
            out.writeInt(numOfObject);
            
            for (Object obj : objects) {
                out.writeObject(obj);
            }
            System.out.println(String.format("%d objects saved", numOfObject));
        }
        
        /**
         * 从存储文件命中读取对象
         * 
         * @param fileName  存储文件名
         * @return Object[] 从文件中读取返回的对象数组
         * 
         * @throws FileNotFoundException
         * @throws IOException
         * @throws ClassNotFoundException
         */
        public static Object[] loadObject(String fileName) throws FileNotFoundException, IOException, ClassNotFoundException{
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
            // 从文件头获取需要读入的对象个数
            int numOfObject = in.readInt();
            System.out.println(String.format("%d objects need to be loaded", numOfObject));
            
            Object[] objects = new Object[numOfObject];
            for(int i=0;i<numOfObject;i++){
                objects[i] = in.readObject();
            }
            return objects;
        }
        
        
        public static void main(String[] args) {
            Object[] person = new Object[1500];
            for(int i=0;i<person.length;i++){
                person[i] = new Person(i, "name"+i);
            }
            
            try {
                saveObject(person, "d:/person.dat");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
            
            try {
                for (Object p : loadObject("d:/person.dat")) {
                    System.out.println((Person)p);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
      

  2.   

    文件头只需要写1次,之后不需要写头信息,请参考下面的代码: long fileSize = new File("E:/a.dat").length();
    if (fileSize > 0) {
    oos = new ObjectOutputStream(new FileOutputStream("E:/a.dat",
    true)) {
    // 重写 writeStreamHeader()方法,空实现
    protected void writeStreamHeader() {
    };
    };
    } else {
    oos = new ObjectOutputStream(new FileOutputStream("E:/a.dat",
    true));
    }
      

  3.   

    去掉 <strong> 标记
      

  4.   

    我不大同意2层的观点,因为你的代码里,第九行后半句:new FileOutputStream("E:/a.dat",true。里面的true,表示允许输出流做追加操作。
    你执行3次之后,30个对象以经全写进去了。(或者,你可以右击属性查看下"E:/a.dat"的字节量,是否增加了)
      

  5.   

    4楼正解
    http://www.cnblogs.com/elleniou/archive/2012/08/29/2662353.html
    还有就是楼主你的异常捕捉完至少要print下不然你都不知道其实程序抛异常了