Como eu estu morto de cansaço porque estou trabalhando em cima disso desde as 10h na manhã, vou postar aqui as classes, para você ver como está e derrepente me dar uma luz sobre como fazer isso.
Classe ProjetoJavaAv2App.jva
package projetojavaav2;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;
public class ProjetoJavaAv2App extends SingleFrameApplication {
@Override protected void startup() {
show(new ProjetoJavaAv2View(this));
}
@Override protected void configureWindow(java.awt.Window root) {
}
public static ProjetoJavaAv2App getApplication() {
return Application.getInstance(ProjetoJavaAv2App.class);
}
public static void main(String[] args) {
launch(ProjetoJavaAv2App.class, args);
}
}
Classe ProjetoJavaAv2View.java
package projetojavaav2;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.Timer;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class ProjetoJavaAv2View extends FrameView {
public ArrayList agenda = new ArrayList();
public ProjetoJavaAv2View(SingleFrameApplication app) {
super(app);
manipularDados md = new manipularDados();
md.abreArquivoParaLeitura();
agenda = md.leObjetosDoArquivo();
md.fechaArquivoLeitura();
initComponents();
ResourceMap resourceMap = getResourceMap();
int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
messageTimer = new Timer(messageTimeout, new ActionListener() {
public void actionPerformed(ActionEvent e) {
statusMessageLabel.setText("");
}
});
messageTimer.setRepeats(false);
int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
for (int i = 0; i < busyIcons.length; i++) {
busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
}
busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
public void actionPerformed(ActionEvent e) {
busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
}
});
idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
statusAnimationLabel.setIcon(idleIcon);
progressBar.setVisible(false);
TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
String propertyName = evt.getPropertyName();
if ("started".equals(propertyName)) {
if (!busyIconTimer.isRunning()) {
statusAnimationLabel.setIcon(busyIcons[0]);
busyIconIndex = 0;
busyIconTimer.start();
}
progressBar.setVisible(true);
progressBar.setIndeterminate(true);
} else if ("done".equals(propertyName)) {
busyIconTimer.stop();
statusAnimationLabel.setIcon(idleIcon);
progressBar.setVisible(false);
progressBar.setValue(0);
} else if ("message".equals(propertyName)) {
String text = (String)(evt.getNewValue());
statusMessageLabel.setText((text == null) ? "" : text);
messageTimer.restart();
} else if ("progress".equals(propertyName)) {
int value = (Integer)(evt.getNewValue());
progressBar.setVisible(true);
progressBar.setIndeterminate(false);
progressBar.setValue(value);
}
}
});
}
@Action
public void showAboutBox() {
if (aboutBox == null) {
JFrame mainFrame = ProjetoJavaAv2App.getApplication().getMainFrame();
aboutBox = new ProjetoJavaAv2AboutBox(mainFrame);
aboutBox.setLocationRelativeTo(mainFrame);
}
ProjetoJavaAv2App.getApplication().show(aboutBox);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
mainPanel = new javax.swing.JPanel();
btInserir = new javax.swing.JButton();
btListar = new javax.swing.JButton();
btAlterar = new javax.swing.JButton();
btLocalizar = new javax.swing.JButton();
btExcluir = new javax.swing.JButton();
btSair = new javax.swing.JButton();
textoNome = new javax.swing.JLabel();
textoCpf = new javax.swing.JLabel();
textoEndereco = new javax.swing.JLabel();
textoTelefone = new javax.swing.JLabel();
nome = new javax.swing.JTextField();
cpf = new javax.swing.JTextField();
endereco = new javax.swing.JTextField();
telefone = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
areaTexto = new javax.swing.JTextArea();
menuBar = new javax.swing.JMenuBar();
javax.swing.JMenu menuArquivo = new javax.swing.JMenu();
javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
javax.swing.JMenu menuAjuda = new javax.swing.JMenu();
javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
statusPanel = new javax.swing.JPanel();
javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
statusMessageLabel = new javax.swing.JLabel();
statusAnimationLabel = new javax.swing.JLabel();
progressBar = new javax.swing.JProgressBar();
mainPanel.setName("mainPanel"); // NOI18N
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(projetojavaav2.ProjetoJavaAv2App.class).getContext().getResourceMap(ProjetoJavaAv2View.class);
btInserir.setText(resourceMap.getString("btInserir.text")); // NOI18N
btInserir.setName("btInserir"); // NOI18N
btInserir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btInserirActionPerformed(evt);
}
});
btListar.setText(resourceMap.getString("btListar.text")); // NOI18N
btListar.setName("btListar"); // NOI18N
btListar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btListarActionPerformed(evt);
}
});
btAlterar.setText(resourceMap.getString("btAlterar.text")); // NOI18N
btAlterar.setName("btAlterar"); // NOI18N
btAlterar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btAlterarActionPerformed(evt);
}
});
btLocalizar.setText(resourceMap.getString("btLocalizar.text")); // NOI18N
btLocalizar.setName("btLocalizar"); // NOI18N
btLocalizar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btLocalizarActionPerformed(evt);
}
});
btExcluir.setText(resourceMap.getString("btExcluir.text")); // NOI18N
btExcluir.setName("btExcluir"); // NOI18N
btExcluir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btExcluirActionPerformed(evt);
}
});
btSair.setText(resourceMap.getString("btSair.text")); // NOI18N
btSair.setName("btSair"); // NOI18N
btSair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btSairActionPerformed(evt);
}
});
textoNome.setText(resourceMap.getString("textoNome.text")); // NOI18N
textoNome.setName("textoNome"); // NOI18N
textoCpf.setText(resourceMap.getString("textoCpf.text")); // NOI18N
textoCpf.setName("textoCpf"); // NOI18N
textoEndereco.setText(resourceMap.getString("textoEndereco.text")); // NOI18N
textoEndereco.setName("textoEndereco"); // NOI18N
textoTelefone.setText(resourceMap.getString("textoTelefone.text")); // NOI18N
textoTelefone.setName("textoTelefone"); // NOI18N
nome.setText(resourceMap.getString("nome.text")); // NOI18N
nome.setName("nome"); // NOI18N
cpf.setText(resourceMap.getString("cpf.text")); // NOI18N
cpf.setName("cpf"); // NOI18N
endereco.setText(resourceMap.getString("endereco.text")); // NOI18N
endereco.setName("endereco"); // NOI18N
telefone.setText(resourceMap.getString("telefone.text")); // NOI18N
telefone.setName("telefone"); // NOI18N
jScrollPane1.setName("jScrollPane1"); // NOI18N
areaTexto.setColumns(20);
areaTexto.setRows(5);
areaTexto.setName("areaTexto"); // NOI18N
jScrollPane1.setViewportView(areaTexto);
javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
mainPanelLayout.setHorizontalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(textoTelefone)
.addComponent(textoNome)
.addComponent(textoCpf)
.addComponent(textoEndereco))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(cpf, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
.addComponent(endereco, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
.addComponent(nome, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
.addComponent(telefone, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE))
.addGap(262, 262, 262))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, mainPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE))
.addGroup(mainPanelLayout.createSequentialGroup()
.addGap(119, 119, 119)
.addComponent(btInserir, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
.addGap(92, 92, 92)
.addComponent(btSair, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE))
.addGroup(mainPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(btExcluir, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(btListar, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)
.addGap(15, 15, 15)
.addComponent(btLocalizar)
.addGap(18, 18, 18)
.addComponent(btAlterar, javax.swing.GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
.addGap(133, 133, 133))
);
mainPanelLayout.setVerticalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(13, 13, 13)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE, false)
.addComponent(btLocalizar)
.addComponent(btListar)
.addComponent(btExcluir)
.addComponent(btAlterar))
.addGap(13, 13, 13)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(textoNome))
.addGap(18, 18, 18)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(cpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(textoCpf))
.addGap(18, 18, 18)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(endereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(textoEndereco)))
.addGroup(mainPanelLayout.createSequentialGroup()
.addGap(111, 111, 111)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(textoTelefone)
.addComponent(telefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btInserir)
.addComponent(btSair))
.addGap(40, 40, 40))
);
menuBar.setName("menuBar"); // NOI18N
menuArquivo.setText(resourceMap.getString("menuArquivo.text")); // NOI18N
menuArquivo.setName("menuArquivo"); // NOI18N
javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(projetojavaav2.ProjetoJavaAv2App.class).getContext().getActionMap(ProjetoJavaAv2View.class, this);
exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
exitMenuItem.setName("exitMenuItem"); // NOI18N
menuArquivo.add(exitMenuItem);
menuBar.add(menuArquivo);
menuAjuda.setText(resourceMap.getString("menuAjuda.text")); // NOI18N
menuAjuda.setName("menuAjuda"); // NOI18N
aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
aboutMenuItem.setName("aboutMenuItem"); // NOI18N
menuAjuda.add(aboutMenuItem);
menuBar.add(menuAjuda);
statusPanel.setName("statusPanel"); // NOI18N
statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N
statusMessageLabel.setName("statusMessageLabel"); // NOI18N
statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N
progressBar.setName("progressBar"); // NOI18N
javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
statusPanel.setLayout(statusPanelLayout);
statusPanelLayout.setHorizontalGroup(
statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(statusPanelLayout.createSequentialGroup()
.addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 11, Short.MAX_VALUE)
.addGroup(statusPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(statusMessageLabel)))
.addGap(255, 255, 255)
.addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(statusAnimationLabel)
.addContainerGap())
);
statusPanelLayout.setVerticalGroup(
statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(statusPanelLayout.createSequentialGroup()
.addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
.addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(statusMessageLabel)
.addComponent(statusAnimationLabel))
.addGap(14, 14, 14))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
setComponent(mainPanel);
setMenuBar(menuBar);
setStatusBar(statusPanel);
}// </editor-fold>
private void btInserirActionPerformed(java.awt.event.ActionEvent evt) {
//valores digitados nos campos
String n = nome.getText(); //obtem valor do campo texto "nome" e guarda na String "n"
String f = telefone.getText();
String c= cpf.getText();
String e=endereco.getText();
//cria objeto para inserir
Pessoa p = new Pessoa( n, Long.parseLong(f),e,Long.parseLong(c));
//insere na coleção
agenda.add(p);
//mensagem
JOptionPane.showMessageDialog(null,
"Inserido com sucesso!");
//limpar campos
nome.setText("");
telefone.setText("");
cpf.setText("");
endereco.setText("");
}
private void btListarActionPerformed(java.awt.event.ActionEvent evt) {
//preparar para listar
StringBuilder lista = new StringBuilder();
//varrer coleção
Iterator aux = agenda.iterator();//aux=referencia auxiliar que aponta para o inicio da estrutura
while(aux.hasNext())
{ Pessoa p = (Pessoa)aux.next();//retorna Object e faz cast para Pessoa
//concatena informações de p e adiciona ao lista
lista.append(p.getNome()+"-"+p.getCpf()+"-"+p.getEndereco()+"-"+p.getFone()+"\n");
}
//obtem String de lista e mostra no listagem
areaTexto.setText(lista.toString());
//limpar campos
nome.setText("");
telefone.setText("");
cpf.setText("");
endereco.setText("");
}
private void btSairActionPerformed(java.awt.event.ActionEvent evt) {
//abrir arquivo gravação
manipularDados manipular = new manipularDados();
manipular.abreArquivoParaGravacao();
//varrer coleção para gravar objetos
Iterator aux = agenda.iterator();//aux=referencia auxiliar que aponta para o inicio da estrutura
while(aux.hasNext())
{ Pessoa p = (Pessoa)aux.next();//retorna Object e faz cast para Pessoa
//grava objeto p no arquivo
manipular.gravaObjetoNoArquivo(p);
}
//fechar arquivo gravação
manipular.fechaArquivoGravacao();
//fechar aplicativo (sem erros)
System.exit(0); //0=sem erros
}
private void btLocalizarActionPerformed(java.awt.event.ActionEvent evt) {
StringBuilder lista = new StringBuilder();
Iterator aux = agenda.iterator();
while(aux.hasNext()){
Pessoa j = (Pessoa)aux.next();
if(j.getNome().indexOf( nome.getText() ) > -1){
lista.append(j.getNome()+"-"+j.getCpf()+"-"+j.getEndereco()+"-"+j.getFone()+"\n");
}
}
areaTexto.setText(lista.toString());
//limpar campos
nome.setText("");
telefone.setText("");
cpf.setText("");
endereco.setText("");
}
private void btExcluirActionPerformed(java.awt.event.ActionEvent evt) {
Iterator aux = agenda.iterator();
while(aux.hasNext()){
Pessoa j = (Pessoa)aux.next();
if(j.getNome().indexOf( nome.getText() ) > -1){
agenda.remove(j);
areaTexto.setText("Cadastro removido com sucesso");
break;
}
}
//limpar campos
nome.setText("");
telefone.setText("");
cpf.setText("");
endereco.setText("");
}
private void btAlterarActionPerformed(java.awt.event.ActionEvent evt) {
String c = cpf.getText();
String f = telefone.getText();
String e = endereco.getText();
StringBuilder lista = new StringBuilder();
Iterator aux = agenda.iterator();
while(aux.hasNext()){
Pessoa j = (Pessoa)aux.next();
if(j.getNome().indexOf( nome.getText() ) > -1){
if( !"".equals(cpf.getText())) {
j.setCpf(Long.parseLong(c));
lista.append(j.getNome()+"-"+j.getCpf()+"-");
}else{
lista.append(j.getNome()+"-"+j.getCpf()+"-");
}
if( !"".equals(endereco.getText())) {
j.setEndereco(e);
lista.append(j.getEndereco()+"-");
}else{
lista.append(j.getEndereco()+"-");
}
if( !"".equals(telefone.getText())) {
j.setFone(Long.parseLong(f));
lista.append(j.getFone());
}else{
lista.append(j.getFone());
}
break;
}
}
areaTexto.setText(lista.toString());
//limpar campos
nome.setText("");
telefone.setText("");
cpf.setText("");
endereco.setText("");
}
private void salvarDados() {
//abrir arquivo gravação
manipularDados manipular = new manipularDados();
manipular.abreArquivoParaGravacao();
//varrer coleção para gravar objetos
Iterator aux = agenda.iterator();//aux=referencia auxiliar que aponta para o inicio da estrutura
while(aux.hasNext())
{ Pessoa p = (Pessoa)aux.next();//retorna Object e faz cast para Pessoa
//grava objeto p no arquivo
manipular.gravaObjetoNoArquivo(p);
}
//fechar arquivo gravação
manipular.fechaArquivoGravacao();
//fechar aplicativo (sem erros)
System.exit(0); //0=sem erros
}
// Variables declaration - do not modify
private javax.swing.JTextArea areaTexto;
private javax.swing.JButton btAlterar;
private javax.swing.JButton btExcluir;
private javax.swing.JButton btInserir;
private javax.swing.JButton btListar;
private javax.swing.JButton btLocalizar;
private javax.swing.JButton btSair;
private javax.swing.JTextField cpf;
private javax.swing.JTextField endereco;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JPanel mainPanel;
private javax.swing.JMenuBar menuBar;
private javax.swing.JTextField nome;
private javax.swing.JProgressBar progressBar;
private javax.swing.JLabel statusAnimationLabel;
private javax.swing.JLabel statusMessageLabel;
private javax.swing.JPanel statusPanel;
private javax.swing.JTextField telefone;
private javax.swing.JLabel textoCpf;
private javax.swing.JLabel textoEndereco;
private javax.swing.JLabel textoNome;
private javax.swing.JLabel textoTelefone;
// End of variables declaration
private final Timer messageTimer;
private final Timer busyIconTimer;
private final Icon idleIcon;
private final Icon[] busyIcons = new Icon[15];
private int busyIconIndex = 0;
private JDialog aboutBox;
}
Classe ManipularDados.java
package projetojavaav2;
import java.io.*;
import java.util.ArrayList;
public class manipularDados {
private ObjectOutputStream saida;
private ObjectInputStream entrada;
public ObjectOutputStream abreArquivoParaGravacao() {
try {
saida = new ObjectOutputStream(
new FileOutputStream("agenda.ser") );
} catch (IOException objErro) {
System.out.println("erro abertura de arquivo:\n\n"+
objErro.getMessage() );
}
return saida;
}
public void gravaObjetoNoArquivo(Pessoa p) {
try
{ saida.writeObject(p);
}
catch (IOException objErro) {
System.out.println("Erro ao gravar para o arquivo:\n\n"+
objErro.getMessage() );
}
}
public void fechaArquivoGravacao() {
try { if (saida != null) {
saida.close();
}
} catch (IOException objErro) {
System.out.println("Erro fechar arquivo:\n\n"+
objErro.getMessage() );
System.exit(1);
}
}
public ObjectInputStream abreArquivoParaLeitura() {
try {
entrada = new ObjectInputStream(
new FileInputStream("agenda.ser"));
} catch (IOException objErro) {
System.err.println("erro abertura de arquivo:\n\n"+
objErro.getMessage() );
}
return entrada;
}
public ArrayList leObjetosDoArquivo() {
Pessoa p = null;
ArrayList lista = new ArrayList();
try
{ while(true){
p = (Pessoa) entrada.readObject();
lista.add(p);
}
}
catch (EOFException objErro) {
System.out.println("fim da leitura.");
}
catch (ClassNotFoundException objErro) {
System.out.println("Não foi possível criar o objeto:\n\n"+
objErro.getMessage() );
}
catch (IOException objErro) {
System.out.println("Erro durante a leitura:\n\n"+
objErro.getMessage() );
}
return lista;
}
public void fechaArquivoLeitura() {
try {
if (entrada != null) {
entrada.close();
}
} catch (IOException objErro) {
System.out.println("Erro fechar arquivo:\n\n"+
objErro.getMessage() );
}
}
}
Classe Pessoa.java
package projetojavaav2;
import java.io.Serializable;
public class Pessoa implements Serializable{
private String nome;
private long fone;
private String endereco;
private long cpf;
public Pessoa(String nome, long fone, String endereco, long cpf) {
this.nome = nome;
this.fone = fone;
this.endereco = endereco;
this.cpf = cpf;
}
public String getNome() {
return nome;
}
public long getFone() {
return fone;
}
public String getEndereco() {
return endereco;
}
public long getCpf() {
return cpf;
}
public void setNome(String nome) {
this.nome = nome;
}
public void setFone(long fone) {
this.fone = fone;
}
public void setEndereco(String endereco) {
this.endereco = endereco;
}
public void setCpf(long cpf) {
this.cpf = cpf;
}
}
Essas são as classes e mais a que eu criei que mostrei no post anterior.
Obrigado novamente.