I am a beginner in android development and i couldn't find out which logic should i apply. I want to perform a login with File Input/Output in java, Now i am able to write text to file, text is "username is admin password is admin" Now when i want to read from this file, i want to apply some login that if the username written in file is "admin" and password written in file is "admin" then show toast, else error. I am not able to figure out that how to read keyword "admin" from this text file. Thanks
4 Answers
Try this,
Create object of sharedPreferences
sharedPreferences = getSharedPreferences("loginDetails", Context.MODE_PRIVATE);Store login details
private void setLoginDetails(String userName, String password) { SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("userName",userName); editor.putString("password",password); editor.commit(); }To check login
private boolean isValidUser(String userName, String password) { if(sharedPreferences.getString("userName",null).equals(userName) && sharedPreferences.getString("password",null).equals(password)) return true; else return false; }
**
Don't save password in sharedPreferences.
** If you really want Its best practice to Encrypt data and save in sharedPreferences. To validate get data from sharedPreferences and Decrypt it.
-
Problem is solved sir, All i wanted was to extract value of username and password from the saved data. i used String.split() method and stored the values in an array and then compared those values with the values from the EditText. – ITypeCode Jan 09 '16 at 13:24
you can use android shared preference or database if you save a file in android sdcard any one can see this file and can modify it
- 1,497
- 1
- 13
- 18
I think you have not done your research. I have just TYPED "how to read from a file in android" in Google and got the answer in FIRST link. You must do research before asking question here.
By using StringBuilder and BufferedReader you can get all TEXT written in file. Store it in String type variable.
Than, You can EASILY find start, last index of searching word from that String type variable by using indexOf(searching word) and lastIndexOf(searching word).
You can get reference of that methods HERE.
By the way below link has your answer. Just check it out.
- 1
- 1
- 1,629
- 2
- 21
- 48
-
Thanks for quick reply and the link Maulik but i had knowledge that how to read data from a file in android, all i wanted to know that how could i extract that specific values of username and password. – ITypeCode Jan 09 '16 at 13:22
-
In that file the **ONLY** text is "username is admin password is admin". Am I right?@user3588320 – Maulik Dodia Jan 11 '16 at 11:34
-
Yeah the only text that was stored was "username is admin password is admin" So i used string.split method and splited the string and stored it into array and then in the textview i saved the value at specified index of that array. – ITypeCode Jan 13 '16 at 04:39
-
Okay. Can you post your answer or you can ACCEPT any answer which has been posted here and you have used.@user3588320 – Maulik Dodia Jan 13 '16 at 05:05
the only text that was stored was "username is admin password is admin" So i used string.split method and splited the string and stored it into array and then in the textview i saved the value at specified index of that array
- 9
- 2