Advertisement
Guest User

Untitled

a guest
Sep 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.35 KB | None | 0 0
  1. package Secuencial;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.DataOutputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9.  
  10. /**
  11.  *
  12.  * @author JJBH
  13.  */
  14. public class OperaEscrituSec extends javax.swing.JFrame {
  15.  
  16.     /** Creates new form OperaEscrituSec */
  17.     public OperaEscrituSec() {
  18.         initComponents();
  19.     }
  20.  
  21.     /** This method is called from within the constructor to
  22.      * initialize the form.
  23.      * WARNING: Do NOT modify this code. The content of this method is
  24.      * always regenerated by the Form Editor.
  25.      */
  26.     @SuppressWarnings("unchecked")
  27.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  28.     private void initComponents() {
  29.  
  30.         jLabel1 = new javax.swing.JLabel();
  31.         jTextField1 = new javax.swing.JTextField();
  32.         jLabel2 = new javax.swing.JLabel();
  33.         jTextField2 = new javax.swing.JTextField();
  34.         jLabel3 = new javax.swing.JLabel();
  35.         jTextField3 = new javax.swing.JTextField();
  36.         jButton1 = new javax.swing.JButton();
  37.  
  38.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  39.         setTitle("Grabación secuencial");
  40.  
  41.         jLabel1.setText("Nombre:");
  42.  
  43.         jLabel2.setText("Apellidos");
  44.  
  45.         jLabel3.setText("Edad");
  46.  
  47.         jButton1.setText("Guardar datos en fichero");
  48.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  49.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  50.                 jButton1ActionPerformed(evt);
  51.             }
  52.         });
  53.  
  54.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  55.         getContentPane().setLayout(layout);
  56.         layout.setHorizontalGroup(
  57.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  58.             .addGroup(layout.createSequentialGroup()
  59.                 .addGap(28, 28, 28)
  60.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  61.                     .addComponent(jButton1)
  62.                     .addGroup(layout.createSequentialGroup()
  63.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  64.                             .addComponent(jLabel1)
  65.                             .addComponent(jLabel2)
  66.                             .addComponent(jLabel3))
  67.                         .addGap(18, 18, 18)
  68.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  69.                             .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
  70.                             .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
  71.                             .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE))))
  72.                 .addContainerGap(65, Short.MAX_VALUE))
  73.         );
  74.         layout.setVerticalGroup(
  75.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  76.             .addGroup(layout.createSequentialGroup()
  77.                 .addGap(51, 51, 51)
  78.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  79.                     .addComponent(jLabel1)
  80.                     .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  81.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  82.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  83.                     .addComponent(jLabel2)
  84.                     .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  85.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  86.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  87.                     .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  88.                     .addComponent(jLabel3))
  89.                 .addGap(61, 61, 61)
  90.                 .addComponent(jButton1)
  91.                 .addContainerGap(93, Short.MAX_VALUE))
  92.         );
  93.  
  94.         pack();
  95.     }// </editor-fold>//GEN-END:initComponents
  96.  
  97.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
  98.         // Declarar un objeto de tipo archivo
  99.         DataOutputStream archivo = null;
  100.  
  101.         // Abir, capturar y grabar datos
  102.         try {
  103.             int edad = 0 ;
  104.  
  105.             // Creando o abriendo para añadir el archivo
  106.             archivo = new DataOutputStream( new FileOutputStream("c:\\secuencial.dat",true) );
  107.  
  108.             // Escribir el nombre y los apellidos
  109.             archivo.writeUTF( jTextField1.getText() );
  110.             archivo.writeUTF( jTextField2.getText() );
  111.             // Obtener la edad
  112.             edad = Integer.parseInt(jTextField3.getText());
  113.             // Escribir la edad
  114.             archivo.writeInt(edad) ;
  115.             // Cerrar fichero
  116.             archivo.close();
  117.  
  118.         } catch(FileNotFoundException fnfe) { /* Archivo no encontrado */ }
  119.         catch (IOException ioe) { /* Error al escribir */ }
  120.         catch (Exception e) { /* Error de otro tipo*/
  121.           System.out.println(e.getMessage());}
  122.  
  123.     }//GEN-LAST:event_jButton1ActionPerformed
  124.  
  125.     /**
  126.     * @param args the command line arguments
  127.     */
  128.     public static void main(String args[]) {
  129.         java.awt.EventQueue.invokeLater(new Runnable() {
  130.             public void run() {
  131.                 new OperaEscrituSec().setVisible(true);
  132.             }
  133.         });
  134.     }
  135.  
  136.     // Variables declaration - do not modify//GEN-BEGIN:variables
  137.     private javax.swing.JButton jButton1;
  138.     private javax.swing.JLabel jLabel1;
  139.     private javax.swing.JLabel jLabel2;
  140.     private javax.swing.JLabel jLabel3;
  141.     private javax.swing.JTextField jTextField1;
  142.     private javax.swing.JTextField jTextField2;
  143.     private javax.swing.JTextField jTextField3;
  144.     // End of variables declaration//GEN-END:variables
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement