简体   繁体   中英

How to copy by reference and deserialize a list of objects in java?

So I am a beginner in Java and my professor has us doing this assignment and I've been looking around but I can't seem to find the right answer for my type of code. We had to create a class file and then a public class file to test it.

import java.io.Serializable;

public class Person implements Serializable {

    String Fname = new String();

    String MI = new String();

    String Lname = new String();

    String  age = new String();
    String gpa = new String();
//      int age = 20;

//      double gpa = 0.0;

    String Major = new String();

    String answer;


 //***********************************************
// The methods declared will go below 
// The first method is for the first name

public String getFname() {
   return Fname;
}

public void setFname(String Fname) {
       this.Fname = Fname;
    }


// This method is for the Middle Initial

public String getMI() {
   return MI;
}

 public void setMI(String MI) {
       this.MI = MI;
    }

// This method is for the Last name

public String getLname() {
       return Lname;    
}

    public void setLname(String Lname) {
       this.Lname = Lname;
    }

 // This method is for the Age

public String getAge() {
   return age;
}

public void setAge(String age) {
       this.age = age;
    }

// This method is for the GPA  

public String getGpa() {
   return gpa;
}

 public void setGpa(String gpa) {
       this.gpa = gpa;
    }

// This method is for the Major

public String getMajor() {
    return Major;
}
 public void setMajor(String Major) {
            this.Major = Major;
    }
}

The Person.java code is Serializable is supposed to be written to a fhm file and then Deserialized in order to print the contents of the file into terminal (Unix). Below is the main code which is the TestPerson code that will read the class Person code.

import java.util.Scanner;
import java.io.*;

public class TestPerson {

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


Person[] Peeps = new Person[3];
Peeps[0] = new Person();
Peeps[1] = new Person();
Peeps[2] = new Person();

Scanner scan = new Scanner(System.in);
String Answer = new String();
String age1 = new String();
String gpa1 = new String();

for (int i = 0; i < Peeps.length - 1; i++) {
//  for (int i = 0; i < 3; i++) {   

System.out.print("What is the First Name? ");
Answer = scan.nextLine();
Peeps[i].setFname(Answer);

System.out.print("What is MI? ");
Answer = scan.nextLine();
Peeps[i].setMI(Answer);

System.out.print(" What is Last Name? ");
Answer = scan.nextLine();
Peeps[i].setLname(Answer);

System.out.print(" What is the Age? ");
age1 = scan.nextLine();
int age = Integer.parseInt(age1);
//  age = scan.nextInt();
Peeps[i].setAge(age1);

System.out.print(" What is the GPA? ");
gpa1 = scan.nextLine();
double gpa = Double.parseDouble(gpa1);
Peeps[i].setGpa(gpa1);

System.out.print(" What is the Major? ");
Answer = scan.nextLine();
Peeps[i].setMajor(Answer);
}

for (int i = 0; i < 3; i++) {
System.out.print(Peeps[i]); 
}

try {
  FileOutputStream fileOut = new FileOutputStream("Peeps.fhm");
  ObjectOutputStream out = new ObjectOutputStream(fileOut);
  out.writeObject(Peeps);
  out.close();
  fileOut.close();
  System.out.println("\nSerialization Successful\n");

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

try { 
  FileInputStream fileIn = new FileInputStream("Peeps.fhm");
  ObjectInputStream in = new ObjectInputStream(fileIn);
  System.out.println("Deserialized Data: \n" + in.readObject().toString());
  in.close();
  fileIn.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
}
}

My professor wants me to prompt the user to enter data for two of the Person objects at the Terminal and then copy the reference to one of two populated Persons to the last Person. Then the program will write all three Persons to a disk file with the extension of ".fhm" (Which I have completed). My question is how do I copy the reference? My second question is also how do I properly deserialize the file because when I run it, it works but the issue that pops up is that it tells me this:

 Deserialized Data: [LPerson;@5e481248 

He wants it to print the information that was inputted by the user. I checked the fhm file that it writes to and it gathers all the information so I'm not sure what I'm doing wrong. Any help would be appreciated guys, sorry the post is kind of long. Thanks in advance.

Your object is basically a Java array. To print it out, you could use:

System.out.println("Deserialized Data: \n" + Arrays.toString(in.readObject()));

I'm not sure what you meant by the "copy the reference" part.

The deserialization seems to be working well. But in your code you're effectively just calling .toString() to print an array, which is probably not what you want. You probably want to iterate over the items and print them one by one:

  FileInputStream fileIn = new FileInputStream("Peeps.fhm");
  ObjectInputStream in = new ObjectInputStream(fileIn);
  Person[] peeps = in.readObject();
  System.out.println("Deserialized Data:");
  for (Person person : peeps) { 
      System.out.printf("First Name: %s MI: %s Last Name: %s%n", 
          person.getFname(), person.getMI(), person.getLname()); 
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM