java序列化错在哪里_如何在不出错的情况下在Java中序列化ArrayLIst?

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

但是当我这样做时,我收到运行时错误“ notSerialisableException:部门。

他们是序列化arrayList的一种特殊方法吗?

有人可以告诉我为什么我会收到此错误。

这是代码:

import java.awt.*;

import java.util.*;

import java.io.*;

import java.io.Serializable;

public class tester1ArrayListObjectSave

{

private ArrayList allDeps = new ArrayList();

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值