Advertisement
BijonDurjoy

Java

Mar 27th, 2022
1,209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.20 KB | None | 0 0
  1. /*
  2.  * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
  3.  * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
  4.  */
  5. package com.bijon.saha;
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.BufferedWriter;
  9. import java.io.File;
  10. import java.io.FileInputStream;
  11. import java.io.FileNotFoundException;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14. import java.io.InputStreamReader;
  15. import java.io.OutputStreamWriter;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18.  
  19.  
  20. public class FileInputOutput extends javax.swing.JFrame {
  21.  
  22.    
  23.     public FileInputOutput()
  24.     {
  25.         initComponents();
  26.        
  27.         this.setTitle("বিজনের নোটপ্যাড ");
  28.         this.setLocationRelativeTo(null);
  29.     }
  30.  
  31.     /**
  32.      * This method is called from within the constructor to initialize the form.
  33.      * WARNING: Do NOT modify this code. The content of this method is always
  34.      * regenerated by the Form Editor.
  35.      */
  36.     @SuppressWarnings("unchecked")
  37.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  38.     private void initComponents() {
  39.  
  40.         jPanel1 = new javax.swing.JPanel();
  41.         openButton = new javax.swing.JButton();
  42.         jScrollPane1 = new javax.swing.JScrollPane();
  43.         textArea = new javax.swing.JTextArea();
  44.         saveButton = new javax.swing.JButton();
  45.         fileName = new javax.swing.JTextField();
  46.  
  47.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  48.         getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
  49.  
  50.         openButton.setText("Open...");
  51.         openButton.addActionListener(new java.awt.event.ActionListener() {
  52.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  53.                 openButtonActionPerformed(evt);
  54.             }
  55.         });
  56.  
  57.         textArea.setColumns(20);
  58.         textArea.setRows(5);
  59.         jScrollPane1.setViewportView(textArea);
  60.  
  61.         saveButton.setText("Save");
  62.         saveButton.addActionListener(new java.awt.event.ActionListener() {
  63.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  64.                 saveButtonActionPerformed(evt);
  65.             }
  66.         });
  67.  
  68.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  69.         jPanel1.setLayout(jPanel1Layout);
  70.         jPanel1Layout.setHorizontalGroup(
  71.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  72.             .addGroup(jPanel1Layout.createSequentialGroup()
  73.                 .addGap(15, 15, 15)
  74.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  75.                     .addComponent(saveButton)
  76.                     .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  77.                         .addGroup(jPanel1Layout.createSequentialGroup()
  78.                             .addComponent(fileName)
  79.                             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  80.                             .addComponent(openButton))
  81.                         .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 340, javax.swing.GroupLayout.PREFERRED_SIZE)))
  82.                 .addGap(15, 15, 15))
  83.         );
  84.         jPanel1Layout.setVerticalGroup(
  85.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  86.             .addGroup(jPanel1Layout.createSequentialGroup()
  87.                 .addGap(16, 16, 16)
  88.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  89.                     .addComponent(openButton, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
  90.                     .addComponent(fileName))
  91.                 .addGap(7, 7, 7)
  92.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
  93.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  94.                 .addComponent(saveButton, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE))
  95.         );
  96.  
  97.         getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 370, 270));
  98.  
  99.         pack();
  100.     }// </editor-fold>                        
  101.  
  102.     private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
  103.         // TODO add your handling code here:
  104.         String fName = fileName.getText();
  105.         File file = new File(fName);
  106.         try {
  107.             BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
  108.             String content = "";
  109.             String temp;
  110.             while((temp=in.readLine())!=null){
  111.                 content+=temp;
  112.                 content+="\n";
  113.             }
  114.             textArea.setText(content);
  115.             in.close();
  116.         } catch (FileNotFoundException ex) {
  117.             Logger.getLogger(FileInputOutput.class.getName()).log(Level.SEVERE, null, ex);
  118.         } catch (IOException ex) {
  119.             Logger.getLogger(FileInputOutput.class.getName()).log(Level.SEVERE, null, ex);
  120.         }
  121.        
  122.        
  123.     }                                          
  124.  
  125.     private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
  126.         // TODO add your handling code here:
  127.         String fName = fileName.getText();
  128.         File file = new File(fName);
  129.         try {
  130.             String text = textArea.getText();
  131.             BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
  132.             out.write(text);
  133.             out.close();
  134.         } catch (FileNotFoundException ex) {
  135.             Logger.getLogger(FileInputOutput.class.getName()).log(Level.SEVERE, null, ex);
  136.         } catch (IOException ex) {
  137.             Logger.getLogger(FileInputOutput.class.getName()).log(Level.SEVERE, null, ex);
  138.         }
  139.     }                                          
  140.  
  141.     /**
  142.      * @param args the command line arguments
  143.      */
  144.     public static void main(String args[])
  145.     {
  146.         /* Set the Nimbus look and feel */
  147.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  148.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  149.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  150.          */
  151.         try {
  152.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  153.                 if ("Nimbus".equals(info.getName())) {
  154.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  155.                     break;
  156.                 }
  157.             }
  158.         } catch (ClassNotFoundException ex) {
  159.             java.util.logging.Logger.getLogger(FileInputOutput.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  160.         } catch (InstantiationException ex) {
  161.             java.util.logging.Logger.getLogger(FileInputOutput.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  162.         } catch (IllegalAccessException ex) {
  163.             java.util.logging.Logger.getLogger(FileInputOutput.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  164.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  165.             java.util.logging.Logger.getLogger(FileInputOutput.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  166.         }
  167.         //</editor-fold>
  168.  
  169.         /* Create and display the form */
  170.         java.awt.EventQueue.invokeLater(() -> {
  171.             new FileInputOutput().setVisible(true);
  172.         });
  173.     }
  174.  
  175.     // Variables declaration - do not modify                    
  176.     private javax.swing.JTextField fileName;
  177.     private javax.swing.JPanel jPanel1;
  178.     private javax.swing.JScrollPane jScrollPane1;
  179.     private javax.swing.JButton openButton;
  180.     private javax.swing.JButton saveButton;
  181.     private javax.swing.JTextArea textArea;
  182.     // End of variables declaration                  
  183. }
  184.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement