Abrir y Crear archivos planos en Java

Código generado con Netbeans 6.1

package calculadora;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class Archivos extends javax.swing.JFrame {

//Creates new form Archivos
public Archivos() {
initComponents();
}

@SuppressWarnings("unchecked")
// //GEN-BEGIN:initComponents
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
btn = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
area = new javax.swing.JTextArea();
btng = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setBackground(new java.awt.Color(0, 0, 0));

btn.setText("BTN");
btn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnActionPerformed(evt);
}
});

area.setColumns(20);
area.setRows(5);
jScrollPane1.setViewportView(area);

btng.setText("BTN2");
btng.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btngActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(jScrollPane1))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addGap(35, 35, 35)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(btng, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btn, javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE))))
.addContainerGap(21, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(30, 30, 30)
.addComponent(btn)
.addGap(18, 18, 18)
.addComponent(btng)
.addContainerGap(50, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);

pack();
}// //GEN-END:initComponents

private void btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnActionPerformed
area.setText("");
JFileChooser fileChooser = new JFileChooser();
int seleccion=fileChooser.showOpenDialog(area);

if (seleccion == JFileChooser.APPROVE_OPTION){
try {
File fichero = fileChooser.getSelectedFile();

BufferedReader reader = new BufferedReader(new FileReader(fichero));    
String linea=""; 
linea = reader.readLine();
while (linea != null){
try {
area.append(linea+"\n");
linea = reader.readLine();

} catch (IOException ex) {
JOptionPane.showMessageDialog(null,"caja linea");
}
}

}catch(IOException ex) {
JOptionPane.showMessageDialog(null,"no lee linea");
}      
}
if(seleccion==JFileChooser.CANCEL_OPTION){

}  


// TODO add your handling code here:
}//GEN-LAST:event_btnActionPerformed

private void btngActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btngActionPerformed

String doc=".doc";
JFileChooser fileChooser = new JFileChooser();
int seleccion = fileChooser.showSaveDialog(area);
if (seleccion == JFileChooser.APPROVE_OPTION)
{
File fichero = fileChooser.getSelectedFile();
try {
fichero.createTempFile("file1", doc, fichero);
} catch (IOException ex) {
Logger.getLogger(Archivos.class.getName()).log(Level.SEVERE, null, ex);
}
PrintWriter writer;
try {
writer = new PrintWriter(fichero);
writer.format(doc, area);
writer.print(area.getText());
writer.close();
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(null,"erro");
}

}
if(seleccion==fileChooser.CANCEL_OPTION){}
// TODO add your handling code here:
}//GEN-LAST:event_btngActionPerformed

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Archivos().setVisible(true);
}
});
}

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextArea area;
private javax.swing.JButton btn;
private javax.swing.JButton btng;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration//GEN-END:variables

}

Post a Comment

Artículo Anterior Artículo Siguiente