You are on page 1of 14

/* * To change this template, choose Tools * and open the template in the editor. */ /* * Form1.

java * * Created on 28/09/2010, 10:53:23 AM */

Templates

package ejemplo3escrituradearchivossecuenciales; import java.io.*; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Students */ public class Form1 extends javax.swing.JFrame { File archivo= new File ("datos.txt"); File origen = new File ("datos.txt"); File destino= new File ("auxiliar.txt"); String nombre, apaterno, amaterno, sexo, edocivil; int edad; float salario; /** Creates new form Form1 */ public Form1() { initComponents(); }

/** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:i nitComponents private void initComponents() { jTextField4 = new javax.swing.JTextField(); buttonGroup1 = new javax.swing.ButtonGroup(); buttonGroup2 = new javax.swing.ButtonGroup(); jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jTextField2 = new javax.swing.JTextField(); jTextField3 = new javax.swing.JTextField(); jTextField5 = new javax.swing.JTextField();

jTextField6 = new javax.swing.JTextField(); jPanel2 = new javax.swing.JPanel(); jRadioButton1 = new javax.swing.JRadioButton(); jRadioButton2 = new javax.swing.JRadioButton(); jPanel3 = new javax.swing.JPanel(); jRadioButton3 = new javax.swing.JRadioButton(); jRadioButton4 = new javax.swing.JRadioButton(); jRadioButton5 = new javax.swing.JRadioButton(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jPanel4 = new javax.swing.JPanel(); jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); jButton4 = new javax.swing.JButton(); jButton5 = new javax.swing.JButton(); jButton6 = new javax.swing.JButton(); jTextField4.setText("jTextField4"); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos")) ; jLabel1.setText("Nombre"); jLabel2.setText("Apellido paterno"); jLabel3.setText("Apellido materno"); jLabel4.setText("Edad"); jLabel5.setText("Salario"); jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Sexo")); buttonGroup1.add(jRadioButton1); jRadioButton1.setSelected(true); jRadioButton1.setText("Masculino"); buttonGroup1.add(jRadioButton2); jRadioButton2.setText("Femenino"); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPan el2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment. LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLay out.Alignment.LEADING) .addComponent(jRadioButton1) .addComponent(jRadioButton2)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX _VALUE)) ); jPanel2Layout.setVerticalGroup(

jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment. LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addComponent(jRadioButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELA TED) .addComponent(jRadioButton2) .addContainerGap(59, Short.MAX_VALUE)) ); jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Estado c ivil")); buttonGroup2.add(jRadioButton3); jRadioButton3.setSelected(true); jRadioButton3.setText("Soltero"); buttonGroup2.add(jRadioButton4); jRadioButton4.setText("Casado"); buttonGroup2.add(jRadioButton5); jRadioButton5.setText("Divorciado"); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPan el3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup( jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment. LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLay out.Alignment.LEADING) .addComponent(jRadioButton3) .addComponent(jRadioButton4) .addComponent(jRadioButton5)) .addContainerGap(26, Short.MAX_VALUE)) ); jPanel3Layout.setVerticalGroup( jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment. LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX _VALUE) .addComponent(jRadioButton3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELA TED) .addComponent(jRadioButton4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRE LATED) .addComponent(jRadioButton5)) ); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPan el1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment. LEADING)

.addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLay out.Alignment.LEADING) .addComponent(jLabel1) .addComponent(jLabel2) .addComponent(jLabel3) .addComponent(jLabel5) .addComponent(jLabel4) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZ E, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) ) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLay out.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(33, 33, 33) .addGroup(jPanel1Layout.createParallelGroup(javax.swing. GroupLayout.Alignment.LEADING, false) .addComponent(jTextField5, javax.swing.GroupLayout.P REFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField6, javax.swing.GroupLayout.P REFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField2) .addComponent(jTextField3, javax.swing.GroupLayout.D EFAULT_SIZE, 165, Short.MAX_VALUE) .addComponent(jTextField1))) .addGroup(jPanel1Layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem ent.RELATED) .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED _SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_S IZE))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX _VALUE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment. LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLay out.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED _SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_S IZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRE LATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLay out.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED _SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_S IZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRE LATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLay out.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED _SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_S

IZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRE LATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLay out.Alignment.BASELINE) .addComponent(jLabel4) .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED _SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_S IZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRE LATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLay out.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem ent.RELATED) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_S IZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jTextField6, javax.swing.GroupLayout.PREFE RRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERR ED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem ent.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED _SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_S IZE) .addGap(38, 38, 38)))) ); jButton1.setText("GUARDAR"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("LIMPIAR"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setText("VER REGISTROS"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Registro s Almacenados")); jTextArea1.setColumns(20); jTextArea1.setRows(5); jScrollPane1.setViewportView(jTextArea1);

javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPan el4); jPanel4.setLayout(jPanel4Layout); jPanel4Layout.setHorizontalGroup( jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment. LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout. createSequentialGroup() .addContainerGap(19, Short.MAX_VALUE) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SI ZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); jPanel4Layout.setVerticalGroup( jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment. LEADING) .addGroup(jPanel4Layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SI ZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(111, Short.MAX_VALUE)) ); jButton4.setText("BUSCAR"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } }); jButton5.setText("ELIMINAR"); jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton5ActionPerformed(evt); } }); jButton6.setText("MODIFICAR"); jButton6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton6ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentP ane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED _SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_S IZE) .addGap(18, 18, 18) .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED _SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_S

IZE)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLa yout.Alignment.TRAILING, false) .addComponent(jButton2, javax.swing.GroupLayout.Alig nment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEF AULT_SIZE, Short.MAX_VALUE) .addComponent(jButton1, javax.swing.GroupLayout.Alig nment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEF AULT_SIZE, Short.MAX_VALUE) .addComponent(jButton3, javax.swing.GroupLayout.Alig nment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLa yout.Alignment.LEADING, false) .addComponent(jButton5, javax.swing.GroupLayout.DEFA ULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton6, javax.swing.GroupLayout.DEFA ULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton4, javax.swing.GroupLayout.DEFA ULT_SIZE, 169, Short.MAX_VALUE)))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX _VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZ E, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.TRA ILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELA TED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.BASELINE) .addComponent(jButton3) .addComponent(jButton4)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.BASELINE) .addComponent(jButton1) .addComponent(jButton5)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.BASELINE) .addComponent(jButton2) .addComponent(jButton6)) .addContainerGap(47, Short.MAX_VALUE)) ); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton1ActionPerformed DataOutputStream dos=null;

nombre= jTextField1.getText(); apaterno= jTextField2.getText(); amaterno= jTextField3.getText(); edad= Integer.parseInt(jTextField5.getText()); salario= Float.parseFloat(jTextField6.getText()); if(jRadioButton1.isSelected()) sexo="Masculino"; else sexo="Femenino"; if(jRadioButton3.isSelected()) edocivil="Soltero"; else if(jRadioButton4.isSelected()) edocivil="casado"; else edocivil="Divorciado"; try{ dos=new DataOutputStream (new FileOutputStream(archivo,true)); dos.writeUTF(nombre); dos.writeUTF(apaterno); dos.writeUTF(amaterno); dos.writeInt(edad); dos.writeFloat(salario); dos.writeUTF(sexo); dos.writeUTF(edocivil); dos.close(); jOptionPane1.showMessageDialog(rootPane, "El registro se guardo exitosame nte","GUARDADO EXITOSO", jOptionPane1.INFORMATION_MESSAGE); jTextField1.setText(""); jTextField2.setText(""); jTextField3.setText(""); jTextField5.setText(""); jTextField6.setText(""); jRadioButton1.setSelected(true); jRadioButton3.setSelected(true); }catch(IOException ioe) { System.out.println("NO se pudieron guardar los datos"); } }//GEN-LAST:event_jButton1ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton2ActionPerformed jTextField1.setText(""); jTextField2.setText(""); jTextField3.setText(""); jTextField5.setText(""); jTextField6.setText(""); jRadioButton1.setSelected(true); jRadioButton3.setSelected(true); jTextArea1.setText(""); }//GEN-LAST:event_jButton2ActionPerformed private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton3ActionPerformed String registros=""; DataInputStream dis=null; try{ dis=new DataInputStream(new FileInputStream(archivo));

while(true){ nombre=dis.readUTF(); apaterno=dis.readUTF(); amaterno=dis.readUTF(); edad=dis.readInt(); salario=dis.readFloat(); sexo=dis.readUTF(); edocivil=dis.readUTF(); registros+=nombre+"\t"+apaterno+"\t"+amaterno+"\t"+String.valueOf(edad)+" \t"+String.valueOf(salario)+"\t"+sexo+"\t"+edocivil+"\n"; } }catch(FileNotFoundException fnfe) { jOptionPane1.showMessageDialog(rootPane, "No se encontro el archivo", "ERROR", jOptionPane1.ERROR_MESSAGE); } catch(EOFException eof){ jOptionPane1.showMessageDialog(rootPane, "Se encontraron registros", "EXITO", jOptionPane1.INFORMATION_MESSAGE); jTextArea1.setText(registros); try{ dis.close(); }catch(IOException ioe){ jOptionPane1.showMessageDialog (rootPane, "No se pudo leer el archivo ", "ERROR", jOptionPane1.ERROR_MESSAGE); } } catch(IOException ioe){ jOptionPane1.showMessageDialog (rootPane, "No se pudo leer el archivo ", "ERROR", jOptionPane1.ERROR_MESSAGE); } }//GEN-LAST:event_jButton3ActionPerformed private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton4ActionPerformed String registro=""; String nombrebuscado = jOptionPane1.showInputDialog(rootPane, "Cual es el nombre que deseas buscar", "DATOS", jOptionPane1.QUESTION_MESSAGE); String esteeselnombrebuscado = jOptionPane1.showInputDialog(rootPane, "Es te es el nombre que buscas", "DATOS", jOptionPane1.QUESTION_MESSAGE); DataInputStream dis=null;

try{ dis=new DataInputStream(new FileInputStream(archivo)); while(true){ nombre=dis.readUTF(); apaterno=dis.readUTF(); amaterno=dis.readUTF(); edad=dis.readInt(); salario=dis.readFloat(); sexo=dis.readUTF(); edocivil=dis.readUTF(); if(nombre.equals(nombrebuscado)){ jOptionPane1.showMessageDialog(rootPane,"SE TUVO EXITO AL ENCONTRAR EL REGISTRO", "BUSQUEDA EXITOSA",jOptionPane1.INFORMATION_MESSAGE); registro=nombre+"\t"+apaterno+"\t"+amaterno+"\t"+String.valueOf(edad)+"

\t"+String.valueOf(salario)+"\t"+sexo+"\t"+edocivil+"\n"; jTextArea1.setText(registro); dis.close(); break; } } }catch(FileNotFoundException fnfe) { jOptionPane1.showMessageDialog(rootPane, "No se encontro el archivo", "ERROR", jOptionPane1.ERROR_MESSAGE); } catch(EOFException eof){ jOptionPane1.showMessageDialog(rootPane, "No se encontro el registro" , "ERROR", jOptionPane1.ERROR_MESSAGE); try{ dis.close(); }catch(IOException ioe){ jOptionPane1.showMessageDialog (rootPane, "No se pudo leer el archivo ", "ERROR", jOptionPane1.ERROR_MESSAGE); } } catch(IOException ioe){ jOptionPane1.showMessageDialog (rootPane, "No se pudo leer el archivo ", "ERROR", jOptionPane1.ERROR_MESSAGE); } }//GEN-LAST:event_jButton4ActionPerformed private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton5ActionPerformed DataInputStream lectura = null; DataOutputStream escritura = null; boolean continuar = false; String nombre_a_eliminar = jOptionPane1.showInputDialog(rootPane, "Cual es el nombre que deseas eliminar", "DATOS", jOptionPane1.QUESTION_MESSAGE); try{ lectura = new DataInputStream(new FileInputStream(origen )); while(true){ nombre=lectura.readUTF(); apaterno=lectura.readUTF(); amaterno=lectura.readUTF(); edad=lectura.readInt(); salario=lectura.readFloat(); sexo=lectura.readUTF(); edocivil=lectura.readUTF(); if(nombre.equals(nombre_a_eliminar)){ continuar=true; break; } } lectura.close(); }catch(FileNotFoundException fnfe){jOptionPane1.showMessageD ialog(rootPane, "No se encontro el archivo", "ERROR", jOptionPane1.ERROR_MESSAGE );} catch(EOFException eofe){jOptionPane1.showMessageDialog(root Pane, "El registro no existe", "ERROR", jOptionPane1.ERROR_MESSAGE);}

catch(IOException oie){jOptionPane1.showMessageDialog(rootPa ne, "Error al intentar abrir el archivo", "ERROR", jOptionPane1.ERROR_MESSAGE);} if(continuar==true){ int respuesta=jOptionPane1.showConfirmDialog(rootPane, " Realmente deseas eliminar el registro???", "CONFIRMACION", jOptionPane1.YES_NO_O PTION, jOptionPane1.QUESTION_MESSAGE); if(respuesta==jOptionPane1.YES_OPTION){ try{ lectura = new DataInputStream(new FileInputStream(origen )); escritura = new DataOutputStream(new FileOutputStream(de stino,true)); while(true){ nombre=lectura.readUTF(); apaterno=lectura.readUTF(); amaterno=lectura.readUTF(); edad=lectura.readInt(); salario=lectura.readFloat(); sexo=lectura.readUTF(); edocivil=lectura.readUTF(); if(!nombre.equals(nombre_a_eliminar)){ escritura.writeUTF(nombre); escritura.writeUTF(apaterno); escritura.writeUTF (amaterno); escritura.writeInt(edad); escritura.writeFloat(salario); escritura.writeUTF(sexo); escritura.writeUTF(edocivil); } } }catch(FileNotFoundException fnfe){jOptionPane1.showMessageD ialog(rootPane, "No se encontro el archivo", "ERROR", jOptionPane1.ERROR_MESSAGE );} catch(EOFException eofe){ try{ lectura.close(); escritura.close(); }catch(IOException ioe){jOptionPane1.showMessageDialog(r ootPane, "Error al intentar eliminar", "ERROR", jOptionPane1.ERROR_MESSAGE);} jOptionPane1.showMessageDialog(rootPane, "Se elimino el registro exitosamente...", "EXITO", jOptionPane1.INFORMATION_MESSAGE); origen.delete(); origen=new File("datos.txt"); destino.renameTo(origen); } catch(IOException oie){jOptionPane1.showMessageDialog(rootPa ne, "Error al intentar abrir el archivo", "ERROR", jOptionPane1.ERROR_MESSAGE);} } } }//GEN-LAST:event_jButton5ActionPerformed private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton6ActionPerformed DataInputStream lectura = null; DataOutputStream escritura = null; boolean continuar = false; String nombre_a_modificar = jOptionPane1.showInputDialog(rootPane, "Cual es el nombre que deseas modificar", "DATOS", jOptionPane1.QUESTION_MESSAGE);

try{ lectura = new DataInputStream(new FileInputStream(origen )); while(true){ nombre=lectura.readUTF(); apaterno=lectura.readUTF(); amaterno=lectura.readUTF(); edad=lectura.readInt(); salario=lectura.readFloat(); sexo=lectura.readUTF(); edocivil=lectura.readUTF(); if(nombre.equals(nombre_a_modificar)){ continuar=true; break; } } lectura.close(); }catch(FileNotFoundException fnfe){jOptionPane1.showMessageD ialog(rootPane, "No se encontro el archivo", "ERROR", jOptionPane1.ERROR_MESSAGE );} catch(EOFException eofe){jOptionPane1.showMessageDialog(root Pane, "El registro no existe", "ERROR", jOptionPane1.ERROR_MESSAGE);} catch(IOException oie){jOptionPane1.showMessageDialog(rootPa ne, "Error al intentar abrir el archivo", "ERROR", jOptionPane1.ERROR_MESSAGE);} if(continuar==true){ int respuesta=jOptionPane1.showConfirmDialog(rootPane, " Realmente deseas modificar el registro???", "CONFIRMACION", jOptionPane1.YES_NO_ OPTION, jOptionPane1.QUESTION_MESSAGE); if(respuesta==jOptionPane1.YES_OPTION){ try{ lectura = new DataInputStream(new FileInputStream(origen )); escritura = new DataOutputStream(new FileOutputStream(de stino,true)); while(true){ nombre=lectura.readUTF(); apaterno=lectura.readUTF(); amaterno=lectura.readUTF(); edad=lectura.readInt(); salario=lectura.readFloat(); sexo=lectura.readUTF(); edocivil=lectura.readUTF(); if(!nombre.equals(nombre_a_modificar)){ escritura.writeUTF(nombre); escritura.writeUTF(apaterno); escritura.writeUTF (amaterno); escritura.writeInt(edad); escritura.writeFloat(salario); escritura.writeUTF(sexo); escritura.writeUTF(edocivil); } else{ nombre=jOptionPane1.showInputDialog(rootPane, "N uevo nombre:", "DATOS", jOptionPane1.QUESTION_MESSAGE); apaterno=jOptionPane1.showInputDialog(rootPane, "Nuevo apellido paterno:", "DATOS", jOptionPane1.QUESTION_MESSAGE); amaterno=jOptionPane1.showInputDialog(rootPane, "Nuevo apellido materno:", "DATOS", jOptionPane1.QUESTION_MESSAGE); edad=Integer.parseInt(jOptionPane1.showInputDial

og(rootPane, "Nueva edad:", "DATOS", jOptionPane1.QUESTION_MESSAGE)); salario=Float.parseFloat(jOptionPane1.showInputD ialog(rootPane, "Nuevo salario:", "DATOS", jOptionPane1.QUESTION_MESSAGE)); String[] sexos=new String[2]; sexos[0]="Masculino"; sexos[1]="Femenino"; int valorsexo=jOptionPane1.showOptionDialog(root Pane, "Nuevo sexo:", "DATOS",jOptionPane1.YES_NO_OPTION , jOptionPane1.QUESTION_ MESSAGE, null, sexos, sexos[0]); if (valorsexo==0) sexo="Masculino"; else sexo="Femenino"; String[] estados=new String[3]; estados[0]="Soltero"; estados[1]="Casado"; estados[2]="Divorciado"; int valorestado=jOptionPane1.showOptionDialog(ro otPane, "Nuevo estado civil:", "DATOS",jOptionPane1.YES_NO_OPTION , jOptionPane1 .QUESTION_MESSAGE, null, estados, estados[0]); if (valorestado==0) edocivil="Soltero"; else if(valorestado == 1) edocivil="Casado"; else edocivil="Divorciado"; escritura.writeUTF(nombre); escritura.writeUTF(apaterno); escritura.writeUTF (amaterno); escritura.writeInt(edad); escritura.writeFloat(salario); escritura.writeUTF(sexo); escritura.writeUTF(edocivil); } } }catch(FileNotFoundException fnfe){jOptionPane1.showMessageD ialog(rootPane, "No se encontro el archivo", "ERROR", jOptionPane1.ERROR_MESSAGE );} catch(EOFException eofe){ try{ lectura.close(); escritura.close(); }catch(IOException ioe){jOptionPane1.showMessageDialog(r ootPane, "Error al intentar modificar", "ERROR", jOptionPane1.ERROR_MESSAGE);} jOptionPane1.showMessageDialog(rootPane, "Se modifico el registro exitosamente...", "EXITO", jOptionPane1.INFORMATION_MESSAGE); origen.delete(); origen=new File("datos.txt"); destino.renameTo(origen); } catch(IOException oie){jOptionPane1.showMessageDialog(rootPa ne, "Error al intentar abrir el archivo", "ERROR", jOptionPane1.ERROR_MESSAGE);} } } }//GEN-LAST:event_jButton6ActionPerformed

// Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.ButtonGroup buttonGroup1; private javax.swing.ButtonGroup buttonGroup2; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JButton jButton5; private javax.swing.JButton jButton6; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JPanel jPanel4; private javax.swing.JRadioButton jRadioButton1; private javax.swing.JRadioButton jRadioButton2; private javax.swing.JRadioButton jRadioButton3; private javax.swing.JRadioButton jRadioButton4; private javax.swing.JRadioButton jRadioButton5; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTextArea1; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; private javax.swing.JTextField jTextField3; private javax.swing.JTextField jTextField4; private javax.swing.JTextField jTextField5; private javax.swing.JTextField jTextField6; // End of variables declaration//GEN-END:variables private javax.swing.JOptionPane jOptionPane1; }

You might also like