java 如何在java中序列化ArrayLIst而不会出错?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7943197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:07:10  来源:igfitidea点击:

How to Serialize an ArrayLIst in java without getting errors?

javaserializationarraylist

提问by Binyomin

I am just trying to output a previously created ArrayList to serialise it for future storage.

我只是想输出一个以前创建的 ArrayList 来序列化它以备将来存储。

but when I attmept to do so I get the runTime error "notSerialisableException: Department.

但是当我尝试这样做时,我收到运行时错误“notSerialisableException:Department。

Is their a speicial way of serializing an arrayList??

它们是序列化 arrayList 的特殊方式吗??

Would someone be able to tell me why I may be getting this error.

有人能告诉我为什么我可能会收到这个错误。

This is the code:

这是代码:

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

}

回答by Brian Roach

ArrayListisn't the problem; your Departmentobject is.

ArrayList不是问题吗?你的Department对象是

You need to implement the Serializableinterface in that object.

您需要Serializable在该对象中实现接口。