Sie sind auf Seite 1von 5

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * filereader.

java * * Created on Jun 19, 2012, 10:39:47 PM */ package desktopapplication1; import import import import import import java.io.BufferedReader; java.io.File; java.io.FileReader; java.io.FileWriter; java.io.PrintWriter; javax.swing.JFileChooser;

/** * * @author Windows */ public class filereader extends javax.swing.JFrame { /** Creates new form filereader */ public filereader() { 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() { jPanel1 = new javax.swing.JPanel(); jButton1 = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jTextField1 = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setName("Form"); // NOI18N jPanel1.setName("jPanel1"); // NOI18N org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.applicat ion.Application.getInstance(desktopapplication1.DesktopApplication1.class).getCo ntext().getResourceMap(filereader.class); jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N jButton1.setName("jButton1"); // NOI18N jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton1ActionPerformed(evt); } }); jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N jLabel1.setName("jLabel1"); // NOI18N jScrollPane1.setName("jScrollPane1"); // NOI18N jTextArea1.setColumns(20); jTextArea1.setRows(5); jTextArea1.setName("jTextArea1"); // NOI18N jScrollPane1.setViewportView(jTextArea1); jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N jButton2.setName("jButton2"); // NOI18N jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N jButton3.setName("jButton3"); // NOI18N jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18 N jTextField1.setName("jTextField1"); // NOI18N javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPan el1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment. LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(79, 79, 79) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SI ZE, 374, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(128, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout. createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLay out.Alignment.TRAILING) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1 Layout.createSequentialGroup() .addGap(104, 104, 104) .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem ent.RELATED, 228, Short.MAX_VALUE) .addComponent(jButton2)) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(jTextField1, javax.swing.GroupLayout.PREFE RRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(79, 79, 79) .addComponent(jButton1))) .addGap(34, 34, 34) .addComponent(jButton3) .addGap(71, 71, 71)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment. LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLay out.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(41, 41, 41) .addGroup(jPanel1Layout.createParallelGroup(javax.swing. GroupLayout.Alignment.BASELINE) .addComponent(jButton1) .addComponent(jButton3)) .addGroup(jPanel1Layout.createParallelGroup(javax.swing. GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(27, 27, 27) .addComponent(jLabel1)) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(37, 37, 37) .addComponent(jButton2)))) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(22, 22, 22) .addComponent(jTextField1, javax.swing.GroupLayout.PREFE RRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERR ED_SIZE))) .addGap(44, 44, 44) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SI ZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(82, Short.MAX_VALUE)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentP ane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.s wing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.s wing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); pack(); }// </editor-fold> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: JFileChooser jf=new JFileChooser("d:\\"); jf.showOpenDialog(this);

File f=jf.getSelectedFile(); String fname=f.getPath(); jLabel1.setText(fname); } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String fn=jLabel1.getText(); try { FileReader fr=new FileReader(fn); BufferedReader br=new BufferedReader(fr); String str; while((str=br.readLine())!=null) { jTextArea1.append(str+"\n"); } fr.close(); br.close(); } catch(Exception e) { System.out.println(e); } } private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: // JFileChooser jf=new JFileChooser("d:\\"); // jf.showSaveDialog(this); // File f=jf.getSelectedFile(); File f1=new File("d:\\xyz"); f1.mkdir(); String fn=f1.getPath()+"\\"+jTextField1.getText(); try { FileWriter fw=new FileWriter(fn); PrintWriter pw=new PrintWriter(fw); String str=jTextArea1.getText(); pw.print(str); pw.close(); fw.close(); } catch(Exception e) { System.out.println(e); } } /** * @param args the command line arguments */ public static void main(String args[]) {

/* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting cod e (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the d efault look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lo okandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIMana ger.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(filereader.class.getName()).log(j ava.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(filereader.class.getName()).log(j ava.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(filereader.class.getName()).log(j ava.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(filereader.class.getName()).log(j ava.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new filereader().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JLabel jLabel1; private javax.swing.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTextArea1; private javax.swing.JTextField jTextField1; // End of variables declaration }

Das könnte Ihnen auch gefallen