Buenas estoy creando un formulario en swich se me ha ocurrido serializzar y deserializar porque tengo que guardarlo en un archivo. el problema es que cuado meto
if (!miFichero.exists ()){
f = new Formulario();
}else {
f = deserializa();
}
me da un error ilegal start of type expected ';' expected package mi fichero do not exit tanto en el if como en el else os dejo el swich completo con el serializa y deserializa
public class NewJFrame extends javax.swing.JFrame {
File miFichero = new File("c:\\lista.dat");
Formulario f = null;
if (!miFichero.exists ()){
f = new Formulario();
}else {
f = deserializa();
}
public NewJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
persona1 = new Clases.Persona();
txtUsuario = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
txtPassword = new javax.swing.JPasswordField();
txtEdad = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
txtTelefono = new javax.swing.JTextField();
btnGuardar = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
jLabel5 = new javax.swing.JLabel();
btnSalir = new javax.swing.JButton();
BtnLimpiar = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(0, 204, 204));
org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, persona1, org.jdesktop.beansbinding.ELProperty.create("${usuario}"), txtUsuario, org.jdesktop.beansbinding.BeanProperty.create("text"));
bindingGroup.addBinding(binding);
txtUsuario.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtUsuarioActionPerformed(evt);
}
});
jLabel1.setText("Usuario");
jLabel2.setText("Password");
binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, persona1, org.jdesktop.beansbinding.ELProperty.create("${password}"), txtPassword, org.jdesktop.beansbinding.BeanProperty.create("text"));
bindingGroup.addBinding(binding);
txtPassword.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
txtPasswordKeyTyped(evt);
}
});
txtEdad.setText("");
binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, persona1, org.jdesktop.beansbinding.ELProperty.create("${edad}"), txtEdad, org.jdesktop.beansbinding.BeanProperty.create("text"));
bindingGroup.addBinding(binding);
txtEdad.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtEdadActionPerformed(evt);
}
});
txtEdad.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
txtEdadKeyTyped(evt);
}
});
jLabel3.setText("Edad");
jLabel4.setText("Telefono");
txtTelefono.setText("");
binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, persona1, org.jdesktop.beansbinding.ELProperty.create("${telefono}"), txtTelefono, org.jdesktop.beansbinding.BeanProperty.create("text"));
bindingGroup.addBinding(binding);
txtTelefono.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtTelefonoActionPerformed(evt);
}
});
txtTelefono.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
txtTelefonoKeyTyped(evt);
}
});
btnGuardar.setText("Guardar");
btnGuardar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnGuardarActionPerformed(evt);
}
});
jPanel1.setBackground(new java.awt.Color(102, 102, 255));
jLabel5.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel5.setText("Formulario");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(34, 34, 34)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 318, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
btnSalir.setText("Salir");
btnSalir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSalirActionPerformed(evt);
}
});
BtnLimpiar.setText("Limpiar");
BtnLimpiar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BtnLimpiarActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(BtnLimpiar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnGuardar)
.addGap(36, 36, 36)
.addComponent(btnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(21, 21, 21))
.addGroup(layout.createSequentialGroup()
.addGap(116, 116, 116)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(txtTelefono, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtEdad, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtPassword, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel3)
.addGap(18, 18, 18)
.addComponent(txtEdad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(11, 11, 11)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnGuardar)
.addComponent(btnSalir)
.addComponent(BtnLimpiar))
.addContainerGap())
);
bindingGroup.bind();
pack();
}// </editor-fold>
private void txtUsuarioActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void txtEdadActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void txtEdadKeyTyped(java.awt.event.KeyEvent evt) {
char c = evt.getKeyChar();
if ((c < '0' || c > '9')) {
evt.consume();
}
}
private void txtTelefonoActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void txtTelefonoKeyTyped(java.awt.event.KeyEvent evt) {
char c = evt.getKeyChar();
if ((c < '0' || c > '9')) {
evt.consume();
}
}
private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
}
private void BtnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {
txtUsuario.setText("");
txtPassword.setText("");
txtEdad.setText("");
txtTelefono.setText("");
}
private void txtPasswordKeyTyped(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
}
private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {
String usuario;
String password;
int edad, telefono;
usuario = txtUsuario.getText();
password = new String(txtPassword.getPassword());
edad = Integer.parseInt(txtEdad.getText());
telefono = Integer.parseInt(txtTelefono.getText());
Persona p = new Persona(usuario, password, edad, telefono);
p.setUsuario(usuario);
p.setPassword(password);
p.setEdad(edad);
p.setTelefono(telefono);
f.aniadirPersona(p);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class
.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class
.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class
.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class
.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton BtnLimpiar;
private javax.swing.JButton btnGuardar;
private javax.swing.JButton btnSalir;
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 Clases.Persona persona1;
private javax.swing.JTextField txtEdad;
private javax.swing.JPasswordField txtPassword;
private javax.swing.JTextField txtTelefono;
private javax.swing.JTextField txtUsuario;
private org.jdesktop.beansbinding.BindingGroup bindingGroup;
// End of variables declaration
private static void serializa(Formulario f) {
FileOutputStream miFos = null;
ObjectOutputStream miOos = null;
try {
String miFichero = "c:\\lista.dat";
miFos = new FileOutputStream(miFichero);
miOos = new ObjectOutputStream(miFos);
miOos.writeObject(f);
miOos.close();
miFos.close();
} catch (Exception ex) {
Logger.getLogger(NewJFrame.class
.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
miFos.close();
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class
.getName()).log(Level.SEVERE, null, ex);
}
try {
miOos.close();
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class
.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static Formulario deserializa() {
FileInputStream miFis = null;
ObjectInputStream miOis = null;
Formulario f = null;
try {
String miFichero = "PilaDePartes.dat";
miFis = new FileInputStream(miFichero);
miOis = new ObjectInputStream(miFis);
boolean seguir = true;
while (seguir) {
try {
f = (Formulario) miOis.readObject();
} catch (ClassNotFoundException ex) {
Logger.getLogger(NewJFrame.class
.getName()).log(Level.SEVERE, null, ex);
} catch (EOFException endOfFileException) {
seguir = false;
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class
.getName()).log(Level.SEVERE, null, ex);
}
}
miOis.close();
miFis.close();
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class
.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
miFis.close();
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class
.getName()).log(Level.SEVERE, null, ex);
}
try {
miOis.close();
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class
.getName()).log(Level.SEVERE, null, ex);
}
}
return f;
}
}