0

Hey guys can u help me on my problem. I'm making a Login Page in java .Where the user will input his/her Username and Email and save it in a text file.. But the problem is I can't save multiple Username and emails In the text file it only save the current username and email of the user.Thanks in advance

   package java_log_in;

     import java.awt.*;
     import javax.swing.*;
     import java.awt.event.*;
     import java.io.*;


  public final class JAVA_FRAME {

   JFrame f=new JFrame("LOGIN PAGE");

   JLabel label=new JLabel("Enter Username");
  JTextField tf=new JTextField(20);

  JLabel label_2=new JLabel("Enter Email");
  JTextField tf_2=new JTextField(20);

JButton button=new JButton("LOG IN");

public JAVA_FRAME(){
     frame();
}

public void frame(){
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(250,250);
    f.setVisible(true);
    f.setLocationRelativeTo(null);

    JPanel panel=new JPanel();
    panel.setBackground(Color.YELLOW);

    panel.add(label);
    panel.add(tf);
    panel.add(label_2);
    panel.add(tf_2);
    panel.add(button);

    f.add(panel);

    event e=new event();
    button.addActionListener(e);
 }

 public class event implements ActionListener{

     @Override
     public void actionPerformed(ActionEvent e){
         try {
             String word=tf.getText();               
             String words=tf_2.getText();  `            
             FileWriter stream= new 
             FileWriter("C://Users//Keyboard//Desktop//file.txt");
             BufferedWriter out = new BufferedWriter(stream); 
             out.write(word);
             out.write(words);
             out.close();
         } catch(Exception ex){}     
     }         
 }

}

BlackLotus
  • 5
  • 1
  • 6
  • Take a closer look at the [JavaDocs](https://docs.oracle.com/javase/8/docs/api/java/io/FileWriter.html#FileWriter-java.io.File-boolean-) – MadProgrammer Oct 27 '17 at 03:22
  • I'd also consider having a look at [`BufferedWriter#newLine`](https://docs.oracle.com/javase/8/docs/api/java/io/BufferedWriter.html#newLine--), it'll come in handy – MadProgrammer Oct 27 '17 at 03:24
  • You might also find [try-with-resources](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) useful – MadProgrammer Oct 27 '17 at 03:25
  • @MadProgrammer..I'll try ur suggestions sir and thank u – BlackLotus Oct 27 '17 at 03:28

0 Answers0