簡體   English   中英

如何通過引用復制並反序列化Java中的對象列表?

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

因此,我是Java的初學者,我的教授讓我們完成了這項任務,而且我一直在尋找,但是我似乎找不到適合我的代碼類型的正確答案。 我們必須先創建一個類文件,然后再創建一個公共類文件來對其進行測試。

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;
    }
}

Person.java代碼是可序列化的,應該被寫入fhm文件,然后反序列化以將文件的內容打印到終端(Unix)中。 下面是主要代碼,這是將讀取類Person代碼的TestPerson代碼。

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();
}
}
}

我的教授希望我提示用戶在終端上輸入兩個Person對象的數據,然后將對兩個填充Person之一的引用復制到最后一個Person。 然后,程序會將所有三個“個人”寫入到擴展名為“ .fhm”的磁盤文件中(已完成)。 我的問題是如何復制參考? 我的第二個問題也是我如何正確反序列化該文件,因為當我運行它時,它可以工作,但是彈出的問題是它告訴我:

 Deserialized Data: [LPerson;@5e481248 

他希望它打印用戶輸入的信息。 我檢查了寫入的fhm文件,它收集了所有信息,所以我不確定自己做錯了什么。 任何幫助將不勝感激的家伙,對不起,這篇文章很長。 提前致謝。

您的對象基本上是一個Java數組。 要打印出來,可以使用:

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

我不確定“復制參考”部分的含義。

反序列化似乎運行良好。 但是在您的代碼中,您實際上只是在調用.toString()來打印數組,這可能不是您想要的。 您可能想要遍歷所有項目並逐一打印:

  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()); 
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM