Abrir y Guardar Archivo de Texto TXT NetBeans

package binario; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import javax.swin

Views 167 Downloads 4 File size 24KB

Report DMCA / Copyright

DOWNLOAD FILE

Recommend stories

Citation preview

package binario; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import javax.swing.JFileChooser; import javax.swing.JOptionPane; /** * * @author gomez */ public class binario1 extends javax.swing.JFrame { JFileChooser seleccionar= new JFileChooser(); File archivo; FileInputStream entrada; FileOutputStream salida; /** * Creates new form binario1 */ public binario1() { initComponents(); } public String AbrirArchivo (File archivo){ String documento =""; try { entrada=new FileInputStream(archivo); int ascci; while((ascci=entrada.read())!=-1){ char caracter=(char)ascci; documento+=caracter; } } catch (Exception e){ } return documento; } public String GuardarArchivo(File archivo,String documento){ String mensaje =null; try { salida=new FileOutputStream(archivo); byte[] bytxt=documento.getBytes(); salida.write(bytxt); mensaje="archivo guardado"; } catch (Exception e){ } return mensaje; } /** * 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") // private void initComponents() { btnabrir = new javax.swing.JButton();

btnguardar = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); txtarea = new javax.swing.JTextArea(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); btnabrir.setText("Abrir Documento"); btnabrir.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnabrirActionPerformed(evt); } }); btnguardar.setText("Guardar Documento"); btnguardar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnguardarActionPerformed(evt); } }); txtarea.setColumns(20); txtarea.setRows(5); jScrollPane1.setViewportView(txtarea); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(25, 25, 25) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 353, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(22, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnabrir) .addGap(18, 18, 18) .addComponent(btnguardar) .addGap(60, 60, 60)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(31, 31, 31) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnabrir) .addComponent(btnguardar)) .addGap(18, 18, 18) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }//

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

}

// TODO add your handling code here: if(seleccionar.showDialog(null, "abrir")==JFileChooser.APPROVE_OPTION){ archivo=seleccionar.getSelectedFile(); if(archivo.canRead()){ if(archivo.getName().endsWith("txt")){ String documento=AbrirArchivo(archivo); txtarea.setText(documento); }else{ JOptionPane.showMessageDialog(null,"Archivo no compatible" ); } } }

private void btnguardarActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: if(seleccionar.showDialog(null, "Guardar")==JFileChooser.APPROVE_OPTION){ archivo=seleccionar.getSelectedFile(); if (archivo.getName().endsWith("txt")){ String Documento=txtarea.getText(); String mensaje=GuardarArchivo(archivo, Documento); if(mensaje!=null){ JOptionPane.showMessageDialog(null, mensaje); }else{ JOptionPane.showConfirmDialog(null, "Archivo no cumpatible"); } }else{ JOptionPane.showMessageDialog(null,"Guardar Documento de Texto"); }

}

} /** * @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(binario1.class.getName()).log(java.util.logging. Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(binario1.class.getName()).log(java.util.logging. Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(binario1.class.getName()).log(java.util.logging. Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(binario1.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 binario1().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton btnabrir; private javax.swing.JButton btnguardar; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea txtarea; // End of variables declaration }