Advertisement
Guest User

my java

a guest
Feb 27th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.73 KB | None | 0 0
  1.  
  2.  
  3.  
  4. /*
  5.  *  Author :  Aashish Poudel
  6.  *  Contact: http://www.facebook.com/mr.aashiz
  7.  */
  8.  
  9. import java.awt.EventQueue;
  10.  
  11. import javax.swing.JButton;
  12. import javax.swing.JFileChooser;
  13. import javax.swing.JFrame;
  14. import javax.swing.JOptionPane;
  15. import javax.swing.JPanel;
  16. import javax.swing.JTextArea;
  17. import javax.swing.border.EmptyBorder;
  18.  
  19. import java.awt.event.ActionListener;
  20. import java.awt.event.ActionEvent;
  21. import java.io.File;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24. import java.io.OutputStreamWriter;
  25.  
  26.  
  27. public class MyWindow extends JFrame {
  28.        
  29.     private JPanel contentPane; //ContentPane to add several components like txtArea and btnSave
  30.  
  31.     /**
  32.      * Launch the application.
  33.      */
  34.     public static void main(String[] args) {
  35.         EventQueue.invokeLater(new Runnable() {
  36.             public void run() {
  37.                 try {
  38.                     MyWindow frame = new MyWindow();
  39.                     frame.setVisible(true);
  40.                 } catch (Exception e) {
  41.                     e.printStackTrace();
  42.                 }
  43.             }
  44.         });
  45.     }
  46.  
  47.     /**
  48.      * Create the frame.
  49.      */
  50.     public MyWindow() {
  51.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  52.         setBounds(100, 100, 450, 300);
  53.         contentPane = new JPanel();
  54.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  55.         setContentPane(contentPane);
  56.         contentPane.setLayout(null);
  57.        
  58.         JTextArea txtArea = new JTextArea();
  59.         txtArea.setText("txtArea");
  60.         txtArea.setBounds(10, 11, 414, 178);
  61.         contentPane.add(txtArea);
  62.        
  63.         JButton btnSave = new JButton("Save");
  64.         btnSave.addActionListener(new ActionListener() {
  65.             public void actionPerformed(ActionEvent arg0) {
  66.                 String filename = JOptionPane.showInputDialog(contentPane,"Enter FileName to Save");
  67.                
  68.                 String textToSave = txtArea.getText();
  69.                 //Opening file to write
  70.                 File file = new File(filename);
  71.                 if(!file.exists()){ //If file does not exists
  72.                     try {
  73.                         file.createNewFile(); // Create new file with the given name in the current directory
  74.                     } catch (IOException e) {
  75.                         // TODO Auto-generated catch block
  76.                         e.printStackTrace(); //If error occured ,, throw error log in Console
  77.                     }
  78.                 }
  79.                 try {
  80.                     OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file)); //Opening the writer to write to the file.. Note that there are other similar classes to do the same
  81.                     //This method writes everything to file
  82.                     for(char c : textToSave.toCharArray()){
  83.                         writer.append(c);
  84.                         writer.flush();
  85.                     }
  86.                 //However this doesnot write new line characters .
  87. //                  writer.write(textToSave);
  88. //                  writer.flush();
  89.                     JOptionPane.showMessageDialog(contentPane, "Success");
  90.                 } catch(IOException ex){
  91.                    
  92.                 }
  93.                
  94.             }
  95.         });
  96.         btnSave.setBounds(335, 200, 89, 23);
  97.         contentPane.add(btnSave);
  98.        
  99.         JButton btnSaveByAnother = new JButton("Save by Another Method");
  100.         btnSaveByAnother.addActionListener(new ActionListener() {
  101.             public void actionPerformed(ActionEvent arg0) {
  102.                 String textToSave = txtArea.getText();
  103.                 JFileChooser fileChooser = new JFileChooser();
  104.                 int x = fileChooser.showSaveDialog(contentPane);
  105.                 if(x!=JFileChooser.CANCEL_OPTION){
  106.                     File file = fileChooser.getSelectedFile();
  107.                     try {
  108.                         //Now Writing to the File
  109.                         OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file)); //Opening the writer to write to the file.. Note that there are other similar classes to do the same
  110.                         //This method writes everything to file
  111.                         for(char c : textToSave.toCharArray()){
  112.                             writer.append(c);
  113.                             writer.flush();
  114.                         }
  115.                         JOptionPane.showMessageDialog(contentPane, "Success");
  116.                     }catch(IOException ex){
  117.                        
  118.                     }
  119.                    
  120.                 }
  121.             }
  122.         });
  123.         btnSaveByAnother.setBounds(10, 228, 153, 23);
  124.         contentPane.add(btnSaveByAnother);
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement