0

I have created a sign up form with an object to store the user details whenever the sign up button is pressed. I have to store the data in a file("ConfidentialAF.txt"). The data appears on the File in chinese characters and when i need to check wether the user exists or not, it does not reads the file.

class New_User implements Serializable 
{
String password, username, name;
New_User()
{

}
/*New_User(String pass, String user, String na )
{
    password = pass;
    username = user;
    name = na;


}*/
 }


 public boolean Username_Check(String uname)
{
    int flag = 0;
    try
    {
        FileInputStream fin = new FileInputStream("ConfidentialAF.txt");
        BufferedInputStream bin = new BufferedInputStream(fin);
        ObjectInputStream ois = new ObjectInputStream(bin);
        New_User temp = new New_User();
        while(true)
        {
            if(temp != null)
            {
                temp =(New_User) ois.readObject();
                if(temp.username == uname)
                {
                    flag = 1;
                    break;
                } 
            }    
            else
            {
                break;
            }

        }
        ois.close();
        bin.close();
        fin.close();
    }
    catch (ClassNotFoundException e)
    {
        e.printStackTrace();
    }
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    if(flag == 0)           //username does not exists
            return false;
        else                //username exists
            return true;
}



private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    New_User n = new New_User();
    n.name = jTextField2.getText();
    n.username = jTextField5.getText();
    String Password_1 = jPasswordField1.getText();
    String Password_2 = jPasswordField2.getText();
    try
    {

        if(Password_1.equals(Password_2) == true && (Username_Check(n.username) == false))
        {
            FileOutputStream fos = new FileOutputStream("ConfidentialAF.txt");
            BufferedOutputStream bout = new BufferedOutputStream(fos);
            ObjectOutputStream oos = new ObjectOutputStream(bout);
            oos.writeObject(n);
            jLabel5.setText("User Created in Database");
            oos.close();
            bout.close();
            fos.close();
        }
        else if(Password_1.equals(Password_2) == false)
        {
            jLabel5.setText("Error, Password not Matching");
        }
        else if(Username_Check(n.username) == true)
        {
            jLabel5.setText("Username Already exists");
        }
        else
        {
            //Do NOTHING
        }

    }
    catch(IOException e)
    {
        e.printStackTrace();
    }

}
Sampurna
  • 205
  • 2
  • 9
  • 2
    When you use an `ObjectOutputStream`, you will not get a text file that you can read with a text editor. Data is stored in a binary format. Also, you are using `==` to compare strings, which will not work. See [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Jesper Jan 16 '19 at 10:24
  • 1
    before you are going into IO actions, first get a good grasp on the basics. You know you can replace: if(Username_Check(n.username) == true) with if(Username_Check(n.username)) and if(Username_Check(n.username) == false) with if(!Username_Check(n.username)), right? – Stultuske Jan 16 '19 at 10:25

0 Answers0