0
I’ve burned my neurons trying to fix it, if anyone can help ; It follows the code that the error occurs. I’m trying to open subclasses that have been serialized, however this error occurs I don’t know why.
try{
fileInput = new FileInputStream("C:\\Users\\Trojers\\Documents\\NetBeansProjects\\Trabalho\\trabalho.txt");
ObjectInputStream ler = new ObjectInputStream(fileInput);
while(true){
ArrayList<Aluno> al = (ArrayList)ler.readObject();
ler.close();
System.out.print(al);
}
} catch (FileNotFoundException ex) {
System.out.printf("Nao encontrado");
System.exit(0);
} catch (IOException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
System.out.printf("Nao encontrado.\n");
System.exit(0);
}
Stracktrace:
java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1563)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:422)
at trabalho.Interface.imprimir_TodosAlunos(Interface.java:201)
at trabalho.Interface.mostrar_Dados(Interface.java:292)
at trabalho.Interface.tela(Interface.java:54)
at trabalho.Trabalho.main(Trabalho.java:18)
The serialization of the objects was made like this:
//Inicializacao dos atributos
Aluno ob = new Pos_Graduacao_Aluno(nome, sobrenome, rga, nomeCurso,
ano_Ingresso, nome_Orient, sob_Nome, nota_Exame); //Aqui, instancio um
objeto do tipo Pos_Graduacao_Aluno, que extende de aluno
cadastrar(ob); //Metodo responsavel por colocar no ArrayList
tela();
} catch(InputMismatchException ex){
System.out.printf("Valor incorreto.\n");
tela();
}
public void cadastrar(Aluno e){ //Metodo que insere no ArrayList
try {
objectOutput = new ObjectOutputStream(fileOutput);
objectOutput.writeObject(e);
} catch (IOException ex) {
System.out.printf("Nao foi possivel salvar o arquivo.\n");
}
}
Also put the code you serialized to make the error playable.
– Isac
You’re serializing multiple objects
Aluno
and trying to deserializeArrayList<Aluno>
. SeveralAluno
followed are not aArrayList<Aluno>
. You really have to wear a bow tie and read from student to student– Isac