import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.JOptionPane; import java.io.*; /** * Main.java * Clase de practica para el manejo de archivos binarios en Java. * * @author Magus * @version 1.0 */ public class Main extends JFrame implements ActionListener { private JButton btnAbrir; private JButton btnGuardar; private JCheckBox chkCasado; private JLabel lblEdad; private JLabel lblFileName; private JLabel lblLine; private JLabel lblNombre; private JLabel lblPeso; private JTextField txtEdad; private JTextField txtFileName; private JTextField txtNombre; private JTextField txtPeso; public Main() { GridBagConstraints gridBagConstraints; lblNombre = new JLabel(); txtNombre = new JTextField(); lblEdad = new JLabel(); txtEdad = new JTextField(); chkCasado = new JCheckBox(); lblPeso = new JLabel(); txtPeso = new JTextField(); lblFileName = new JLabel(); txtFileName = new JTextField(); lblLine = new JLabel(); btnAbrir = new JButton(); btnGuardar = new JButton(); getContentPane().setLayout(new GridBagLayout()); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); lblNombre.setLabelFor(txtNombre); lblNombre.setText("Nombre"); gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 2; gridBagConstraints.fill = GridBagConstraints.BOTH; gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 1.0; getContentPane().add(lblNombre, gridBagConstraints); gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 2; gridBagConstraints.gridwidth = 3; gridBagConstraints.fill = GridBagConstraints.BOTH; gridBagConstraints.weightx = 3.0; gridBagConstraints.weighty = 1.0; getContentPane().add(txtNombre, gridBagConstraints); lblEdad.setText("Edad"); gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 3; gridBagConstraints.fill = GridBagConstraints.BOTH; gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 1.0; getContentPane().add(lblEdad, gridBagConstraints); gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 3; gridBagConstraints.gridwidth = 3; gridBagConstraints.fill = GridBagConstraints.BOTH; gridBagConstraints.weightx = 3.0; gridBagConstraints.weighty = 1.0; getContentPane().add(txtEdad, gridBagConstraints); chkCasado.setText("\u00bfCasado?"); gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 5; gridBagConstraints.gridwidth = 4; gridBagConstraints.fill = GridBagConstraints.BOTH; gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 1.0; getContentPane().add(chkCasado, gridBagConstraints); lblPeso.setText("Peso"); gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 4; gridBagConstraints.fill = GridBagConstraints.BOTH; gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 1.0; getContentPane().add(lblPeso, gridBagConstraints); gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 4; gridBagConstraints.gridwidth = 3; gridBagConstraints.fill = GridBagConstraints.BOTH; gridBagConstraints.weightx = 3.0; gridBagConstraints.weighty = 1.0; getContentPane().add(txtPeso, gridBagConstraints); lblFileName.setLabelFor(txtNombre); lblFileName.setText("Nombre de Archivo"); gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.fill = GridBagConstraints.BOTH; gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 1.0; getContentPane().add(lblFileName, gridBagConstraints); gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 0; gridBagConstraints.gridwidth = 3; gridBagConstraints.fill = GridBagConstraints.BOTH; gridBagConstraints.weightx = 3.0; gridBagConstraints.weighty = 1.0; getContentPane().add(txtFileName, gridBagConstraints); lblLine.setBackground(new Color(255, 0, 0)); lblLine.setFocusable(false); gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.gridwidth = 4; gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 0.5; getContentPane().add(lblLine, gridBagConstraints); btnAbrir.setText("Abrir"); btnAbrir.addActionListener(this); gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridy = 6; gridBagConstraints.gridwidth = 2; gridBagConstraints.fill = GridBagConstraints.BOTH; gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 1.0; getContentPane().add(btnAbrir, gridBagConstraints); btnGuardar.setText("Guardar"); btnGuardar.addActionListener(this); gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridy = 6; gridBagConstraints.gridwidth = 2; gridBagConstraints.fill = GridBagConstraints.BOTH; gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 1.0; getContentPane().add(btnGuardar, gridBagConstraints); setSize(300,500); setVisible(true); setTitle("Manejo de Archivos"); } /** * @param args argumentos de la lina de comandos */ public static void main(String[] args) { Main app = new Main(); } /** * Metodo que responde a eventos, se va a encargar de manejar los archivos. * En caso de que el boton presionado fuera el boton de abrir abre el archivo * que se ha puesto en la linea de archivo, en caso de que el boton presionado * fuera el de guardar guarda los datos de la forma al archivo en la linea * de nombre de archivo. * * @param e Evento que sucedio. */ public void actionPerformed(ActionEvent e) { String fileName = txtFileName.getText(); if (e.getSource() == btnAbrir) { try { /** * Primero tengo que crear un objeto de tipo DataInputStream * para poder abir archivos, despues tengo que ir leyendo * todos los datos mediatne los metodos para leer de * archivos binarios e irlos copiando a los controles. */ DataInputStream fileIn = new DataInputStream(new FileInputStream(fileName)); String nombre = fileIn.readUTF(); int edad = fileIn.readInt(); double peso = fileIn.readDouble(); boolean isCasado = fileIn.readBoolean(); txtNombre.setText(nombre); txtEdad.setText(String.valueOf(edad)); txtPeso.setText(String.valueOf(peso)); chkCasado.setSelected(isCasado); } catch (FileNotFoundException fnfe) { JOptionPane.showMessageDialog(null, "No encuentro el archivo!", "Error", JOptionPane.ERROR_MESSAGE); } catch (IOException ioe) { JOptionPane.showMessageDialog(null, "No puedo leer el archivo!", "Error", JOptionPane.ERROR_MESSAGE); } } else if (e.getSource() == btnGuardar) { try { DataOutputStream fileOut = new DataOutputStream(new FileOutputStream(fileName)); fileOut.writeUTF(txtNombre.getText()); fileOut.writeInt( Integer.parseInt(txtEdad.getText()) ); fileOut.writeDouble( Double.parseDouble(txtPeso.getText()) ); fileOut.writeBoolean(chkCasado.isSelected()); } catch (FileNotFoundException fnfe) { JOptionPane.showMessageDialog(null, "No encuentro el archivo!", "Error", JOptionPane.ERROR_MESSAGE); } catch (IOException ioe) { JOptionPane.showMessageDialog(null, "No puedo leer el archivo!", "Error", JOptionPane.ERROR_MESSAGE); } } } }