如何序列在java中的ArrayList没有得到错误? [英] How to Serialize an ArrayLIst in java without getting errors?
本文介绍了如何序列在java中的ArrayList没有得到错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是想输出pviously创建一个ArrayList的$ P $以连载它未来的存储。
但是当我attmept这样做,我得到的运行时错误notSerialisableException:系
时的序列化的ArrayList ??的speicial方式
会有人能告诉我为什么我会收到此错误。
这是code:
进口java.awt中的*。
进口的java.util。*;
进口java.io. *;
的Bean; 公共类tester1ArrayListObjectSave
{ 私人的ArrayList<&系GT; allDeps =新的ArrayList<&系GT;();
私人INT选择= 0;
私人字符串名称;
私人字符串禄;
部theDepartment;
扫描程序扫描; 公共静态无效的主要(字串[] args)
{ 新tester1ArrayListObjectSave(); } 公共tester1ArrayListObjectSave()
{
扫描=新的扫描仪(System.in);
选项();
} 公共无效选项()
{
的System.out.println(wadya想做的事); 的System.out.println(1创建一个新的部门);
的System.out.println(2从文本文件中读取);
的System.out.println(4将它保存到系统作为序列化的文件。);
的System.out.println(从文本文件中读取。);
的System.out.println(3退出。); 选择= scan.nextInt();
workOutOptions(); } 公共无效workOutOptions()
{
如果(选择== 1)
{
createNewEmp();
}
否则,如果(选择== 2)
{
尝试
{
readTextToSystem();
}
赶上(IOException异常EXC)
{
的System.out.println(呃哦,他们是一个错误:+ EXC);
}
}
否则,如果(选择== 3)
{
System.exit(0);
}
否则,如果(选择== 4)
{
尝试
{
createSerialisable();
}
赶上(IOException异常EXC)
{
的System.out.println(对不起未能连载的这一数据的原因:+ EXC);
}
}
其他
{
的System.out.println(什么都不做);
}
}
公共无效createNewEmp()
{
的System.out.println(叫什么名字);
名称= scan.next();
的System.out.println(什么是CHAPS LOC);
LOC = scan.next();
尝试
{
saveToSystem();
}
赶上(IOException异常EXC)
{
//做一些事情在这里处理问题
}
theDepartment =新部门(姓名,LOC); allDeps.add(theDepartment); 选项();
} 公共无效saveToSystem()抛出IOException异常
{
FOS的FileOutputStream =新的FileOutputStream(为Backup.txt,真正的);
为PrintStream不过outFile =新的PrintStream(FOS);
的System.out.println(添加到系统成功地);
outFile.println(名);
outFile.println(LOC);
outFile.close();
选项();
} 公共无效readTextToSystem()抛出IOException异常
{
扫描仪INFILE =新的扫描仪(新的文件(为Backup.txt));
而(inFile.hasNextLine())
{
名称= inFile.nextLine();
的System.out.println(这样的名字:+姓名);
LOC = inFile.nextLine();
的System.out.println(这样的位置:+ LOC);
部dDepartment =新部门(姓名,LOC);
allDeps.add(dDepartment);
选项(); }
的System.out.println(allDeps);
} 公共无效createSerialisable()抛出IOException异常
{
FileOutputStream中FILEOUT =新的FileOutputStream(theBkup.ser);
ObjectOutputStream的出=新的ObjectOutputStream(FILEOUT);
out.writeObject(allDeps);
选项();
}}
解决方案
的ArrayList
是没有问题的;你的处
对象。
您需要实现在该对象的序列化
接口。
I am just trying to output a previously created ArrayList to serialise it for future storage.
but when I attmept to do so I get the runTime error "notSerialisableException: Department.
Is their a speicial way of serializing an 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();
}
}
解决方案
ArrayList
isn't the problem; your Department
object is.
You need to implement the Serializable
interface in that object.
这篇关于如何序列在java中的ArrayList没有得到错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文