3

私の 2 つのクラスは、すべての変数を出力するための toString オーバーライドを含む StudentData オブジェクト (名前、生年月日、および ID) の配列を作成するように設計されています。次に、配列をシリアル化し、studentdata.txt という名前のファイルに保存します。次に、ファイルから配列を読み取り、このデータから新しい配列オブジェクトを再構築して、配列内の各項目をコンソールに出力します。

準拠しようとすると、このエラーが発生します...

Exception in thread "main" java.io.NotSerializableException: Student
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeArray(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at StudentData.main(StudentData.java:38)

また、配列を適切にループし、toString メソッドを呼び出してコンソールに出力する方法もわかりません。for each ループを使用する必要があると仮定してもよろしいでしょうか? このような?

                 //for (Student s : ???) {
                  //System.out.println(How do I call toString from here?);

私のクラス

import java.io.*;         //importing input-output files
    class Student
    {


   String name;                     //declaration of variables
 String DOB;
   int id;



   Student(String naam,int idno, String dob)          //Initialising variables to user data
   { 
          name=naam;
          id=idno;
          DOB=dob;

    }

   public String toString() {
       return name+"\t"+id+"\t"+DOB+"\t";
}



}

2番

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

class StudentData                     //main class
{
  public static void main(String args[]) throws IOException                  //exception handling
  {
         System.out.println("Enter the numbers of students:");
         BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
         int n=Integer.parseInt(in.readLine());


         Student[]  S=new Student[n];                      // array of objects declared and defined
         for (int i = 0; i < S.length; i++)       {

               System.out.println("Enter the Details of Student no: "+(i+1));             //reading data form the user
               System.out.println("Name: ");
               String naam=in.readLine();
               System.out.println("ID no: ");
               int idno=Integer.parseInt(in.readLine());
               System.out.println("DOB: ");               
               String dob=(in.readLine());


              S[i]=new Student(naam,idno,dob);                          

              File studentFile = new File("StudentData.txt");
              try {
              FileOutputStream fileOutput = new FileOutputStream(studentFile);
              ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput);
              objectOutput.writeObject(S);

              S = null;

              FileInputStream fileInput = new FileInputStream(studentFile);
              ObjectInputStream objectInputStream = new ObjectInputStream(fileInput);

                S = (Student[]) objectInputStream.readObject();
            } catch (ClassNotFoundException e) {

                e.printStackTrace();
            }

              //for (Student s : ???) {
                  //System.out.println();
              }

         }








     }
4

3 に答える 3

3

NotSerializableException: Student例外を取り除くには、クラスにインターフェイスをStudent実装するだけです。Serializable(これはマーカー インターフェースであるため、メソッドを実装する必要はありません。)

class Student implements Serializable {
}

オブジェクトの配列をループするにはS、次を試してください。Student

for (Student st : S) {
    System.out.println(st);
}

ただし、配列にはよりわかりやすい名前を選択します(studentsより良いでしょう)。配列を次のように宣言すると、

Student[]  students = new Student[n];

そうすれば、for ループが読みやすくなります。

for (Student student : students) {
    System.out.println(student);
}

toStringtype のオブジェクトでメソッドを明示的に呼び出す必要はないことに注意してくださいStudent。を期待するメソッドへのパラメーターとしてオブジェクトを使用すると、Java は暗黙的にこれを行いますString

于 2012-10-18T17:06:52.313 に答える
2

クラスは、そのインスタンスを作成するためのインターフェイスをStudent実装する必要があります。Serializableserializable

クラス宣言を次のように編集しますStudent: -

class Student implements java.io.Serializable {
}

このリンクを参照してください: - http://docs.oracle.com/javase/7/docs/platform/serialization/spec/serial-arch.html について詳しく説明していSerializationます。

于 2012-10-18T17:02:17.427 に答える
1

Student クラスの java.io.Serializable インターフェイスを実装します。

于 2012-10-18T17:04:32.083 に答える