Advertisement
TermSpar

PrintWriters

Jul 31st, 2016
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import java.io.FileNotFoundException;
  4. import java.io.PrintWriter;
  5.  
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.JOptionPane;
  9. import javax.swing.JTextField;
  10.  
  11.  
  12. public class FileTut {
  13.  
  14.     //Frame Objects:
  15.     private JFrame frame = new JFrame();
  16.     private JTextField txtData = new JTextField();
  17.     private JButton btnWrite = new JButton();
  18.    
  19.     //File Vars:
  20.     PrintWriter output;
  21.     String path;
  22.    
  23.     public FileTut() {
  24.         //Frame Attributes:
  25.         frame.setSize(400, 200);
  26.         frame.setLayout(null);
  27.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28.         frame.setVisible(true);
  29.         frame.setResizable(false);
  30.        
  31.         //txtData Attributes:
  32.         txtData.setSize(400, 20);
  33.         txtData.setLocation(0, 0);
  34.        
  35.         //btnWrite Attributes:
  36.         btnWrite.setSize(150, 50);
  37.         btnWrite.setText("Write");
  38.         btnWrite.setLocation(120, 30);
  39.        
  40.         //btnWrite Click Event:
  41.         btnWrite.addActionListener(new ActionListener(){
  42.             public void actionPerformed(ActionEvent arg0) {
  43.                 path = JOptionPane.showInputDialog("Enter Path ");
  44.                 try {
  45.                     output = new PrintWriter(path);
  46.                     output.println(txtData.getText());
  47.                     output.close();
  48.                     JOptionPane.showMessageDialog(null, "Data Successfully Written To " + path);
  49.                 } catch (FileNotFoundException e) {
  50.                     JOptionPane.showMessageDialog(null, "Could not find path " + path);
  51.                 }
  52.             }
  53.         });
  54.        
  55.         //Add Frame Objects:
  56.         frame.add(txtData);
  57.         frame.add(btnWrite);
  58.     }
  59.    
  60.     public static void main(String[] args){
  61.         new FileTut();
  62.     }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement