Java 对象列表为ArrayList的writeObject上的ObjectOutputStream ioexception
提前感谢您的回复。 我正试图将对象的ArrayList保存到文件中,但继续获取IOExeption。 守则:Java 对象列表为ArrayList的writeObject上的ObjectOutputStream ioexception,java,Java,提前感谢您的回复。 我正试图将对象的ArrayList保存到文件中,但继续获取IOExeption。 守则: public static final String FILE_PATH=""; // is set to blank for tests. It DOES create a file. public static void saveToFileCl(ArrayList<cliente> al ) { if (al != null ) { try {
public static final String FILE_PATH=""; // is set to blank for tests. It DOES create a file.
public static void saveToFileCl(ArrayList<cliente> al ) {
if (al != null ) {
try {
FileOutputStream file = new FileOutputStream(FILE_PATH+"cliente.dat");
ObjectOutputStream outStream = new ObjectOutputStream(file);
outStream.writeInt(cliente.getAuto_inc()); // works fine
outStream.writeObject(new Date()); // works fine - not needed just for testing!
outStream.writeObject(al); // exception !!!!
outStream.flush();
outStream.close();
} catch (IOException e) {System.out.println("Erro a gravar ficheiro de clientes!\n"+e.getCause());}
catch (Exception e) {System.out.println("Erro a desconhecido ao gravar ficheiro de clientes!\n"+e.getMessage());}
}
}
公共静态最终字符串文件_PATH=”“;//对于测试,设置为空。它确实创建了一个文件。
公共静态无效saveToFileCl(ArrayList al){
如果(al!=null){
试一试{
FileOutputStream文件=新的FileOutputStream(文件路径+“cliente.dat”);
ObjectOutputStream outStream=新的ObjectOutputStream(文件);
outStream.writeInt(client.getAuto_inc());//工作正常
outStream.writeObject(new Date());//工作正常-不需要仅用于测试!
outStream.writeObject(al);//异常!!!!
冲水;
exptream.close();
}catch(IOException e){System.out.println(“Erro a gravar ficheiro de clientes!\n”+e.getCause());}
catch(异常e){System.out.println(“客户的严重错误!\n”+e.getMessage());}
}
}
有人能告诉我为什么会出现异常吗?要使用
OutputStream.writeObject(Object)
将实例写入OutputStream,您需要实例是可序列化的。这就是那个接口(java.io.Serializable),它告诉我们一个实例可以以字节形式存储
您只需要将接口添加到类中
class MyClass implements java.io.Serializable { ... }
没有可以实现的方法,只有这一行就足够了。请添加完整的StackTraceFill错误堆栈跟踪pleaseIscliente
serializable?告诉我stacktrace告诉你完全相同的想法…不,它是不可序列化的。主要是因为我不知道这是必须的。这是什么?我从哪里可以得到更多信息?可能是重复的