Dpo3 U1 A2 Anec
Dpo3 U1 A2 Anec
Dpo3 U1 A2 Anec
3 UNIDAD 1
Actividad 2:
Lectura de
archivos
Matricula: ES172001980
package archivotexto;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
/**
Matricula:ES172001980
**/
//creacion de la clase
public class texto extends javax.swing.JFrame
{ JFileChooser seleccionar=new
FileInputStream entrada;
FileOutputStream salida;
public texto()
{ initComponents(
);
String documento="";
try{
entrada=new FileInputStream(archivo);
int ascci;
while ((ascci=entrada.read())!=-1){
char caracter=(char)ascci;
documento+=caracter;
return 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
*/
@SuppressWarnings("unchecked")
jScrollPane1 = new
javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
Botonabrir.addActionListener(new java.awt.event.ActionListener() {
BotonabrirActionPerformed(evt);
});
botonguardar.setText("Guardar");
botonguardar.addActionListener(new java.awt.event.ActionListener() {
botonguardarActionPerformed(evt);
});
txtarea.setColumns(20);
txtarea.setRows(5);
jScrollPane1.setViewportView(txtarea);
jButton1.setText("Cerrar");
jButton1.addActionListener(new java.awt.event.ActionListener() {
jButton1ActionPerformed(evt);
});
jLabel1.setFont(new java.awt.Font("Tahoma", 2, 12)); // NOI18N
getContentPane().setLayout(layout);
layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLa
yout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane1)
.addGroup(layout.createSequentialGroup()
.addComponent(Botonabrir)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42,
Short.MAX_VALUE)
.addComponent(jButton1)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addComponent(jLabel1)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
);
layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayo
ut.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24,
Short.MAX_VALUE)
.addComponent(jLabel1)
.addContainerGap())
);
pack();
}// </editor-fold>
//metodo para que el boton pueda guardar
if(seleccionar.showDialog(null,"Guardar")==JFileChooser.APPROVE_OPTION){
archivo=seleccionar.getSelectedFile();
if(archivo.getName().endsWith("txt"))
{ String
Documento=txtarea.getText();
if(mensaje!=null){
JOptionPane.showMessageDialog(null, mensaje);
}else{
}else{
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 {
System.exit(WIDTH);
/**
*/
//<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.
*/
try {
if ("Nimbus".equals(info.getName()))
{ javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
{ java.util.logging.Logger.getLogger(texto.class.getName()).log(java.util.logging.Level.SEVER
E,
null, ex);
{ java.util.logging.Logger.getLogger(texto.class.getName()).log(java.util.logging.Level.SEVER
E,
null, ex);
{ java.util.logging.Logger.getLogger(texto.class.getName()).log(java.util.logging.Level.SEVER
E,
null, ex);
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
new texto().setVisible(true);
});
}
Bibliografía