反序列化文件时不检索所有对象

时间:2014-03-12 06:15:56

标签: java sockets serversocket

通过

序列化数据
try {
  FileOutputStream fileOut = new FileOutputStream(
      "C:\\Users\\saikiran\\Documents\\NetBeansProjects\\FTP\\reg.ser", true);
  ObjectOutputStream out = new ObjectOutputStream(fileOut);
  out.writeObject(r);
  out.close();
  fileOut.close();
  System.out.printf("Serialized data is saved in /tmp/reg.ser");
  pr.println("Registered Successfully ");
} catch (IOException i) {
  i.printStackTrace();
}

并且反序列化不使整个文件对象只获得单个对象,即仅起始对象。

 FileInputStream fileIn = new FileInputStream("C:\\Users\\saikiran\\Documents\\NetBeansProjects\\FTP\\reg.ser");
                   ObjectInputStream in = null;                       
while (fileIn.available() != 0) {

  in = new ObjectInputStream(fileIn);
  while (in != null && in.available() != 0) {
    r = (Registration) in.readObject();
    System.out.println("Logged in :" + "User name :" + r.u + "Password  " + r.p);

    if (r.u.equals(ur) && r.p.equals(ps)) {
      System.out.println("Logged in :" + "User name :" + r.u + "Password  " + r.p);
      pr.println("Display");

    }
  }
}

1 个答案:

答案 0 :(得分:0)

我已经为您创建了工作样本。

我的POJO serializable课程将是,

import java.io.Serializable;

public class Pojo implements Serializable{

    String name;
    String age;
    String qualification;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getQualification() {
        return qualification;
    }
    public void setQualification(String qualification) {
        this.qualification = qualification;
    }

}

我的main课程将是,

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class Serialization {

    /**
     * @param args
     */
    public static final String FILENAME = "F:\\test\\cool_file.ser";

    public static void main(String[] args) throws IOException, ClassNotFoundException {


        FileOutputStream fos = null;
        //ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream(FILENAME);
            //oos = new ObjectOutputStream(fos);
/*          for (String s : test.split("\\s+")) {
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(s);
            }*/

            for(int i=0;i<10;i++){
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                Pojo pojo = new Pojo();
                pojo.setName("HumanBeing - "+i);
                pojo.setAge("25 - "+i);
                pojo.setQualification("B.E - "+i);
                oos.writeObject(pojo);
            }

        } finally {
            if (fos != null)
                fos.close();
        }

        List<Object> results = new ArrayList<Object>();

        FileInputStream fis = null;
        //ObjectInputStream ois = null;

        try {
            fis = new FileInputStream(FILENAME);
            //ois = new ObjectInputStream(fis);
            while (true) {
                ObjectInputStream ois = new ObjectInputStream(fis);
                results.add(ois.readObject());
            }
        } catch (EOFException ignored) {
            // as expected
        } finally {
            if (fis != null)
                fis.close();
        }
        System.out.println("results = " + results);

        for (int i=0; i<results.size()-1; i++) {
            System.out.println(((Pojo)results.get(i)).getName()+ "    "+((Pojo)results.get(i)).getAge()+ "    "+((Pojo)results.get(i)).getQualification());
        }
    }

}

希望它有所帮助。