Last active
August 29, 2015 14:01
-
-
Save werbet/58491ef0f56251f0e1d3 to your computer and use it in GitHub Desktop.
Deserialization code with multiple objects into a single file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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