Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Author : Aashish Poudel
- * Contact: http://www.facebook.com/mr.aashiz
- */
- import java.awt.EventQueue;
- import javax.swing.JButton;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextArea;
- import javax.swing.border.EmptyBorder;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStreamWriter;
- public class MyWindow extends JFrame {
- private JPanel contentPane; //ContentPane to add several components like txtArea and btnSave
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- MyWindow frame = new MyWindow();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public MyWindow() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 450, 300);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- JTextArea txtArea = new JTextArea();
- txtArea.setText("txtArea");
- txtArea.setBounds(10, 11, 414, 178);
- contentPane.add(txtArea);
- JButton btnSave = new JButton("Save");
- btnSave.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- String filename = JOptionPane.showInputDialog(contentPane,"Enter FileName to Save");
- String textToSave = txtArea.getText();
- //Opening file to write
- File file = new File(filename);
- if(!file.exists()){ //If file does not exists
- try {
- file.createNewFile(); // Create new file with the given name in the current directory
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace(); //If error occured ,, throw error log in Console
- }
- }
- try {
- 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
- //This method writes everything to file
- for(char c : textToSave.toCharArray()){
- writer.append(c);
- writer.flush();
- }
- //However this doesnot write new line characters .
- // writer.write(textToSave);
- // writer.flush();
- JOptionPane.showMessageDialog(contentPane, "Success");
- } catch(IOException ex){
- }
- }
- });
- btnSave.setBounds(335, 200, 89, 23);
- contentPane.add(btnSave);
- JButton btnSaveByAnother = new JButton("Save by Another Method");
- btnSaveByAnother.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- String textToSave = txtArea.getText();
- JFileChooser fileChooser = new JFileChooser();
- int x = fileChooser.showSaveDialog(contentPane);
- if(x!=JFileChooser.CANCEL_OPTION){
- File file = fileChooser.getSelectedFile();
- try {
- //Now Writing to the File
- 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
- //This method writes everything to file
- for(char c : textToSave.toCharArray()){
- writer.append(c);
- writer.flush();
- }
- JOptionPane.showMessageDialog(contentPane, "Success");
- }catch(IOException ex){
- }
- }
- }
- });
- btnSaveByAnother.setBounds(10, 228, 153, 23);
- contentPane.add(btnSaveByAnother);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement