Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JTextField;
- public class FileTut {
- //Frame Objects:
- private JFrame frame = new JFrame();
- private JTextField txtData = new JTextField();
- private JButton btnWrite = new JButton();
- //File Vars:
- PrintWriter output;
- String path;
- public FileTut() {
- //Frame Attributes:
- frame.setSize(400, 200);
- frame.setLayout(null);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- frame.setResizable(false);
- //txtData Attributes:
- txtData.setSize(400, 20);
- txtData.setLocation(0, 0);
- //btnWrite Attributes:
- btnWrite.setSize(150, 50);
- btnWrite.setText("Write");
- btnWrite.setLocation(120, 30);
- //btnWrite Click Event:
- btnWrite.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent arg0) {
- path = JOptionPane.showInputDialog("Enter Path ");
- try {
- output = new PrintWriter(path);
- output.println(txtData.getText());
- output.close();
- JOptionPane.showMessageDialog(null, "Data Successfully Written To " + path);
- } catch (FileNotFoundException e) {
- JOptionPane.showMessageDialog(null, "Could not find path " + path);
- }
- }
- });
- //Add Frame Objects:
- frame.add(txtData);
- frame.add(btnWrite);
- }
- public static void main(String[] args){
- new FileTut();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement