Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Khan2ndSemAPL;
- import java.awt.Container;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import javax.swing.*;
- public class Activity_32_Writing_toText_FileOutputStream extends JFrame implements ActionListener
- {
- FileOutputStream fswriter;
- public JLabel l1, l2;
- public JTextField t1, t2;
- public JButton b1, b2;
- Activity_32_Writing_toText_FileOutputStream()
- {
- Container c = getContentPane();
- c.setLayout(new GridLayout(3, 2));
- l1 = new JLabel("Customer Login Name: ");
- l2 = new JLabel("Password: ");
- t1 = new JTextField();
- t2 = new JPasswordField();
- b1 = new JButton("Login");
- b2 = new JButton("Clear");
- c.add(l1);
- c.add(l2);
- c.add(t1);
- c.add(t2);
- c.add(b1);
- c.add(b2);
- b1.addActionListener(this);
- b2.addActionListener(this);
- pack();
- setVisible(true);
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- this.setLocationRelativeTo(null);
- }
- public void actionPerformed(ActionEvent ae)
- {
- if(ae.getSource() == b1)
- {
- if(t1.getText().isEmpty() || t1.getText().trim().equals("")
- || t2.getText().isEmpty() || t2.getText().trim().equals(""))
- {
- System.err.println("details are incomplete");
- JOptionPane.showMessageDialog(null, "Details are incomplete");
- }
- else
- {
- try
- {
- fswriter = new FileOutputStream("C:\\Users\\10-0235C\\Documents\\migs\\login.text", false);
- String temp = "PASSWORD: " + t1.getText() + " USERNAME: " + t2.getText();
- //String temp = t1.getText() + t2.getText();
- //System.out.println(temp.getBytes());
- //System.out.println(temp.getBytes());
- fswriter.write(temp.getBytes());
- System.out.println("finished writing details to file");
- JOptionPane.showMessageDialog(null, "finished writing details to file");
- fswriter.close();
- }
- catch(FileNotFoundException e)
- {
- System.out.println("cannot find login.txt file " + e.toString());
- e.printStackTrace();
- }
- catch(IOException e)
- {
- System.out.println("Error writing to file " + e.toString());
- e.printStackTrace();
- }
- }
- }
- if(ae.getSource() == b2)
- {
- t1.setText(null);
- t2.setText(null);
- }
- }
- public static void main(String args[])
- {
- Activity_32_Writing_toText_FileOutputStream e = new Activity_32_Writing_toText_FileOutputStream();
- }
- }
Add Comment
Please, Sign In to add comment