registro y búsqueda

hola comunidad, tengo un ligero problema estoy haciendo un formulario pero al registrar un nuevo dato no guarda y se borrar el dato anterior, alguien podría ayudarme a ver mi error? por favor...

dejo el código...

package finalpro;
import java.awt.Color;
import java.io.File;
import java.io.FileWriter;
import javax.swing.JTextField;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.*;

class Maestros extends javax.swing.JFrame {
public String nombre=" ";
public String apellido=" ";
public String especialidad=" ";
public String matricula=" ";

public Maestros() {
this.getContentPane().setBackground(Color.white);
initComponents();
setLocationRelativeTo(null);
setResizable(false);
setTitle("Maestro");
}

@SuppressWarnings("unchecked")
//
private void initComponents() {

Titulo2M = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
NombreM = new javax.swing.JTextField();
ApellidosM = new javax.swing.JTextField();
EspecialidadM = new javax.swing.JTextField();
MatriculaM = new javax.swing.JTextField();
RegistrarM = new javax.swing.JButton();
BuscarM = new javax.swing.JButton();
OrdenarM = new javax.swing.JButton();
RegresarM = new javax.swing.JButton();
SalirM = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

Titulo2M.setText("Ingrese los siguientes datos para registrar un MAESTRO:");

jLabel1.setText("Nombre(s):");

jLabel2.setText("Apellidos:");

jLabel3.setText("Especialidad:");

jLabel4.setText("Matricula:");

NombreM.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
NombreMActionPerformed(evt);
}
});

ApellidosM.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ApellidosMActionPerformed(evt);
}
});

RegistrarM.setText("Registrar");
RegistrarM.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
RegistrarMActionPerformed(evt);
}
});

BuscarM.setText("Buscar");
BuscarM.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BuscarMActionPerformed(evt);
}
});

OrdenarM.setText("Ordenar");

RegresarM.setText("Regresar");
RegresarM.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
RegresarMActionPerformed(evt);
}
});

SalirM.setText("Salir");
SalirM.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
SalirMMouseClicked(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(RegistrarM)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(BuscarM)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(OrdenarM)
.addGap(26, 26, 26)
.addComponent(RegresarM)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(SalirM))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(EspecialidadM, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(NombreM)
.addComponent(ApellidosM, javax.swing.GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE)))))
.addComponent(Titulo2M)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel4)
.addGap(39, 39, 39)
.addComponent(MatriculaM, javax.swing.GroupLayout.PREFERRED_SIZE, 259, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(28, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(Titulo2M, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(NombreM, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(ApellidosM, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(MatriculaM, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(EspecialidadM, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(RegistrarM)
.addComponent(BuscarM)
.addComponent(OrdenarM)
.addComponent(RegresarM)
.addComponent(SalirM))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

pack();
}//

private void ApellidosMActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void RegresarMActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Principal obj=new Principal();
obj.setVisible(true);
dispose();
}

private void SalirMMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
private void RegistrarMActionPerformed(java.awt.event.ActionEvent evt) {
nombre=NombreM.getText();
NombreM.setText(nombre);
apellido=ApellidosM.getText();
ApellidosM.setText(apellido);
matricula=MatriculaM.getText();
MatriculaM.setText(matricula);
especialidad=EspecialidadM.getText();
EspecialidadM.setText(especialidad);
try(FileWriter fw=new FileWriter("Doc/1.txt");
FileReader fr=new FileReader("Doc/1.txt")){
//Escribimos en el fichero un String y un caracter 97 (a)
fw.write(nombre);
fw.write(92);
fw.write(apellido);
fw.write(92);
fw.write(matricula);
fw.write(92);
fw.write(especialidad);

//Guardamos los cambios del fichero
fw.flush();
//Leemos el fichero y lo mostramos por pantalla
int valor=fr.read();
while(valor!=-1){
System.out.print((char)valor);
valor=fr.read();
}
}catch(IOException e){
System.out.println("Error E/S: "+e);
}
}
private void BuscarMActionPerformed(java.awt.event.ActionEvent evt) {

}

private void NombreMActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* 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(Maestros.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Maestros.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Maestros.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Maestros.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Maestros().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JTextField ApellidosM;
private javax.swing.JButton BuscarM;
private javax.swing.JTextField EspecialidadM;
private javax.swing.JTextField MatriculaM;
private javax.swing.JTextField NombreM;
private javax.swing.JButton OrdenarM;
private javax.swing.JButton RegistrarM;
private javax.swing.JButton RegresarM;
private javax.swing.JButton SalirM;
private javax.swing.JLabel Titulo2M;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
// End of variables declaration
}

Comentarios

Opciones de visualización de comentarios

Seleccione la forma que prefiera para mostrar los comentarios y haga clic en «Guardar las opciones» para activar los cambios.

Re: registro y búsqueda

 

Cambia esta línea:

try(FileWriter fw=new FileWriter("Doc/1.txt");

por:

try(FileWriter fw=new FileWriter("Doc/1.txt", true);

~~~