2011-10-30 3 views
1

私は、以前作成したArrayListを出力して、将来のストレージ用にシリアル化しようとしています。エラーを発生させずにjavaでArrayLIstをシリアライズするには?

が、私はそうするattmeptとき、私はランタイムエラー「notSerialisableExceptionを取得:。部門

は、誰かが、なぜ私がかもしれない私に言うことができるだろう??

をArrayListのをシリアル化の彼らのspeicial方法です。このエラーを取得すること

これはコードです:

import java.awt.*; 
    import java.util.*; 
    import java.io.*; 
    import java.io.Serializable; 

    public class tester1ArrayListObjectSave 
    { 

    private ArrayList <Department> allDeps = new ArrayList<Department>(); 
    private int choice = 0; 
    private String name; 
    private String loc; 


    Department theDepartment; 
    Scanner scan; 

    public static void main(String[] args) 
    { 

    new tester1ArrayListObjectSave();  

    } 

    public tester1ArrayListObjectSave() 
    { 
      scan = new Scanner(System.in); 
      options(); 
    } 

    public void options() 
    { 
     System.out.println("wadya wanna do"); 



     System.out.println("1. create a new department"); 
     System.out.println("2. read from text file"); 
     System.out.println("4. save it to system as a serializable file"); 
     System.out.println(". read from text file"); 
     System.out.println("3. to exit"); 

     choice = scan.nextInt(); 
     workOutOptions(); 

    } 

    public void workOutOptions() 
    { 
     if (choice ==1) 
     { 
      createNewEmp(); 
     } 
     else if (choice ==2) 
     { 
      try 
      { 
      readTextToSystem(); 
      } 
      catch (IOException exc) 
      { 
       System.out.println("uh oh their was an error: "+exc); 
      } 
     } 
     else if (choice == 3) 
     { 
      System.exit(0); 
     } 
     else if (choice ==4) 
     { 
      try 
      { 
      createSerialisable(); 
      } 
      catch (IOException exc) 
      { 
       System.out.println("sorry could not serialise data cause of this:"+exc); 
      } 
     } 
     else 
     { 
      System.out.println("do nothing"); 
     } 
    } 


    public void createNewEmp() 
    { 


      System.out.println("What is the name"); 
      name = scan.next(); 
      System.out.println("what is the chaps loc"); 
      loc = scan.next(); 
      try 
      { 
       saveToSystem(); 
      } 
       catch (IOException exc) 
      { 
      // do something here to deal with problems 
      } 
      theDepartment = new Department(name,loc); 

      allDeps.add(theDepartment); 

      options(); 
    } 

    public void saveToSystem() throws IOException 
    { 
     FileOutputStream fos = new FileOutputStream("backUp.txt", true); 
     PrintStream outFile = new PrintStream(fos); 
     System.out.println("added to system succesfully"); 
     outFile.println(name); 
     outFile.println(loc); 
     outFile.close(); 
     options();  
    } 

    public void readTextToSystem() throws IOException 
    { 
     Scanner inFile = new Scanner (new File ("backUp.txt")); 
     while (inFile.hasNextLine()) 
     { 
     name=inFile.nextLine(); 
     System.out.println("this is the name: "+name); 
     loc = inFile.nextLine(); 
     System.out.println("this is the location: "+loc); 
     Department dDepartment = new Department(name,loc); 
     allDeps.add(dDepartment); 
     options(); 

     } 
     System.out.println(allDeps); 
    } 

    public void createSerialisable() throws IOException 
    { 
     FileOutputStream fileOut = new FileOutputStream("theBkup.ser"); 
     ObjectOutputStream out = new ObjectOutputStream(fileOut); 
     out.writeObject(allDeps); 
     options(); 
    } 

} 
+4

'Department'は' Serializable'インタフェースを実装する必要があります。 – blackcompe

+0

その意味は、コード内のオブジェクト部門は、 として宣言する必要があります。public class deparmentは、serializableを実装していますか? – Binyomin

+0

うんのおかげで、大丈夫動作しているようになりました – Binyomin

答えて

7

ArrayListは問題ではありません。あなたのDepartmentオブジェクトはです。

Serializableインターフェイスをそのオブジェクトに実装する必要があります。

+0

うんのおかげでその今、イムは、まだ私はこの ArrayListのような何かを書くと、配列リストをdeserialiseするために、この直列化可能なもの – Binyomin

+0

を学ぶすべての良い theDeps =新しいArrayListを(); \t \t \t \t theDeps =(ArrayList)in.readObject(); – Binyomin

関連する問題