Advertisement
xlujiax

JFRAME LEER Y ESCRIBIR

Jul 4th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.65 KB | None | 0 0
  1. --JFRAME LEER Y ESCRIBIR
  2. package archivos;
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.BufferedWriter;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileReader;
  8. import java.io.FileWriter;
  9. import java.io.IOException;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12.  
  13. /**
  14.  *
  15.  * @author luisgarcia
  16.  */
  17. public class JFArchivo extends javax.swing.JFrame {
  18.  
  19.     /**
  20.      * Creates new form NewJFrame
  21.      */
  22.     public JFArchivo() {
  23.         initComponents();
  24.     }
  25.  
  26.     /**
  27.      * This method is called from within the constructor to initialize the form.
  28.      * WARNING: Do NOT modify this code. The content of this method is always
  29.      * regenerated by the Form Editor.
  30.      */
  31.     @SuppressWarnings("unchecked")
  32.     private void jBLeerActionPerformed(java.awt.event.ActionEvent evt) {                                      
  33.         // TODO add your handling code here:
  34.        
  35.         String nombre = JTNombreArchivo.getText();
  36.        
  37.         try {
  38.             FileReader fr = new FileReader(nombre);
  39.            
  40.             BufferedReader br = new BufferedReader(fr);
  41.            
  42.             String line="";
  43.             String tota="";
  44.             while((line=br.readLine())!=null){
  45.                 tota+=line+"\n"; // por cada linea leida agregamos un salto de linea
  46.             }
  47.  
  48.             jTextArchivo.setText(tota);
  49.            
  50.         } catch (FileNotFoundException ex) {
  51.             Logger.getLogger(JFArchivo.class.getName()).log(Level.SEVERE, null, ex);
  52.         } catch (IOException ex) {
  53.             Logger.getLogger(JFArchivo.class.getName()).log(Level.SEVERE, null, ex);
  54.         }
  55.        
  56.     }                                      
  57.  
  58.     private void jBEscribirActionPerformed(java.awt.event.ActionEvent evt) {                                          
  59.         // TODO add your handling code here:
  60.         String nombre = JTNombreArchivo.getText();
  61.         String info = " ";
  62.         try {
  63.             FileWriter fw = new FileWriter(nombre);
  64.             BufferedWriter bw = new BufferedWriter(fw);
  65.             info = jTextArchivo.getText();
  66.             bw.write(info);
  67.             bw.newLine();  
  68.             bw.close();
  69.                
  70.         } catch (IOException e) {
  71.             e.printStackTrace();
  72.         }
  73.     }                                          
  74.  
  75.     /**
  76.      * @param args the command line arguments
  77.      */
  78.     public static void main(String args[]) {
  79.         /* Set the Nimbus look and feel */
  80.         java.awt.EventQueue.invokeLater(new Runnable() {
  81.             public void run() {
  82.                 new JFArchivo().setVisible(true);
  83.             }
  84.         });
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement