Foros aprenderaprogramar.com
Aprender a programar => Aprender a programar desde cero => Mensaje iniciado por: victor garay en 24 de Abril 2015, 04:02
-
Estoy guardando los datos de pacientes en un archivo binario: numero de expediente, dni, apellidos, nombres, telefono, direccion y fotografia.
El problema se me presenta cuando busco a los pacientes x su numero de expediente, solo me muestra los datos del ultimo paciente ingresados, los anteriores desaparecen, al parecer el problema se presenta cuando se guarda los datos se sobreescribe y no los agrega al archivo binario. aqui les dejo los codigos de guardar y buscar respectivamente a ver si me ayudan. gracias:
private void btcGuardarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Object nombreArchivo = archivo;
System.out.println(nombreArchivo);
try{
ObjectOutputStream fileout = new ObjectOutputStream(new FileOutputStream((String) nombreArchivo));
fileout.writeObject(txtNroExpediente.getText());
fileout.writeObject(txtDni.getText());
fileout.writeObject(txtApellidos.getText());
fileout.writeObject(txtNombres.getText());
fileout.writeObject(txtDireccion.getText());
fileout.writeObject(txtTelefono.getText());
fileout.writeObject(lblFoto.getIcon());
fileout.flush();
JOptionPane.showMessageDialog(null, "Los datos del paciente se guardaron corecttamente...");
if(fileout!=null){
fileout.close();
}
}catch(IOException e){}
desactivarTextFields();
btcGuardar.setEnabled(false);
btcNuevo.setEnabled(true);
btcBuscar.setEnabled(false);
}
private void btcBuscarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Object nombreArchivo = archivo;
try{
try (ObjectInputStream filein = new ObjectInputStream(new FileInputStream((String) nombreArchivo))){
Object expediente = filein.readObject();
Object dni = filein.readObject();
Object apellidos = filein.readObject();
Object nombres = filein.readObject();
Object direccion = filein.readObject();
Object telefono = filein.readObject();
Object foto = filein.readObject();
if (txtNroExpediente.getText().equals(expediente)){
txtNroExpediente.setText((String) expediente);
txtDni.setText((String) dni);
txtApellidos.setText((String) apellidos);
txtNombres.setText((String) nombres);
txtDireccion.setText((String) direccion);
txtTelefono.setText((String) telefono);
lblFoto.setIcon((Icon) foto);
}
if(filein!=null){
filein.close();
}
} catch (ClassNotFoundException ex) {
Logger.getLogger(JDPacientes.class.getName()).log(Level.SEVERE, null, ex);
}
}catch(IOException e){}
}
-
Hola Víctor, para pegar código hazlo siguiendo lo que se indica aquí: https://www.aprenderaprogramar.com/foros/index.php?topic=1460.0
Para poder revisar haría falta poder compilar el código (o el código completo o un fragmento que crees simplemente a modo de prueba), con lo que has puesto ahora mismo es difícil determinar dónde puede estar el problema.
Es posible que se solucione llamando ObjectOutputStream.reset() cuando terminas de escribir los datos en el fichero, pero habría que estudiarlo.
Saludos