You are on page 1of 3

package plf.

funcion;
import
import
import
import
import
import
import
import

java.io.BufferedReader;
java.io.File;
java.io.FileReader;
java.io.FileWriter;
java.io.IOException;
javax.swing.JFileChooser;
javax.swing.JOptionPane;
javax.swing.filechooser.FileNameExtensionFilter;

/**
*
* @author Kauasha
*/
public class interfaz extends javax.swing.JFrame {
String texto;
JFileChooser fileChooser;
public void abrirArchivo() {
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filtro = new FileNameExtensionFilter("txt (*.txt
)", "txt");
int seleccion = fileChooser.showOpenDialog(this);
fileChooser.setFileFilter(filtro);
if (seleccion == JFileChooser.APPROVE_OPTION) {
File fichero = fileChooser.getSelectedFile();
try (FileReader fr = new FileReader(fichero)) {
BufferedReader lee = new BufferedReader(fr);
String cadena = "", aux = "";
while ((aux = lee.readLine()) != null) {
cadena += aux + "\n";
}
areaDeTexto.setText(cadena);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public void guardarArchivo() {
String datos=nombre.getText()+ " \n"+numero.getText()+" \n"+carrera.ge
tText()+ " \n" + fecha.getText();
try {
String nombre = "";
JFileChooser file = new JFileChooser();
file.showSaveDialog(this);
File guarda = file.getSelectedFile();
if (guarda != null) {
nombre = file.getSelectedFile().getName();
FileWriter save = new FileWriter(guarda + ".txt");
save.write(datos);
save.close();

JOptionPane.showMessageDialog(null,
" guardado Exitosamente",
"GUARDANDO..", JOptionPane.INFORMATION_MESSAGE);
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(null,
"Su archivo no se ha guardado",
"error", JOptionPane.WARNING_MESSAGE);
}
nombre.setText("");
numero.setText("");
carrera.setText("");
fecha.setText("");
}
/**
* Creates new form interfaz
*/
public interfaz() {
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">
private void initComponents() {
nombre = new javax.swing.JTextField();
numero = new javax.swing.JTextField();
carrera = new javax.swing.JTextField();
fecha = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
areaDeTexto = new javax.swing.JTextArea();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jButton3 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("GUARDAR");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("ABRIR");
jButton2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {


jButton2ActionPerformed(evt);
}
});
areaDeTexto.setColumns(20);
areaDeTexto.setRows(5);
jScrollPane1.setViewportView(areaDeTexto);
jLabel1.setText("NOMBRE");
jLabel2.setText("NUMERO CONTROL");
jLabel3.setText("CARRERA");
jLabel4.setText("FECHA");
jButton3.setText("SALIR");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
abrirArchivo();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
guardarArchivo();
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JOptionPane.showMessageDialog(null, "USTED SALDRA DEL PROGRAMA");
System.exit(1);
}

You might also like