Skip to content

Instantly share code, notes, and snippets.

@werbet
Last active August 29, 2015 14:01
Show Gist options
  • Save werbet/58491ef0f56251f0e1d3 to your computer and use it in GitHub Desktop.
Save werbet/58491ef0f56251f0e1d3 to your computer and use it in GitHub Desktop.
Deserialization code with multiple objects into a single file.
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;
public class Deserialization {
public static void main(String[] args) throws Exception {
ObjectInputStream entrada = null;
String nomeArquivo = "arquivo.binario";
System.out.println("--------------------------------------------");
entrada = new ObjectInputStream(new FileInputStream(nomeArquivo));
ler(entrada);
ler(entrada);
ler(entrada);
entrada.close();
}
private static void ler(ObjectInputStream entrada) throws Exception
{
ClasseExemplo aux;
aux = (ClasseExemplo) entrada.readObject();
aux.imprimir();
}
}
class ClasseExemplo implements Serializable
{
private static final long serialVersionUID = 1L;
private String nome;
private int id;
void setar(int id, String nome)
{
this.id = id;
this.nome = nome;
}
void imprimir()
{
System.out.println("ID = " + id);
System.out.println("Nome = " + nome);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment