Added read password from file.

This commit is contained in:
Marcin Wozniak 2019-10-17 13:40:41 +02:00
parent f36506d92e
commit 7ac96f9de4

View File

@ -12,15 +12,40 @@ import android.widget.EditText;
import android.widget.TextView;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
TextView response;
Button button;
Button restartpassword;
private EditText textPassword;
private View viewPassword;
private static final String filename = "passwd.txt";
public String readFile(String filename) throws IOException {
String content = null;
File file = new File(filename); // For example, foo.txt
FileReader reader = null;
try {
reader = new FileReader(file);
char[] chars = new char[(int) file.length()];
reader.read(chars);
content = new String(chars);
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(reader != null){
reader.close();
}
}
return content;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -42,14 +67,19 @@ public class MainActivity extends AppCompatActivity {
});
button.setOnClickListener(new OnClickListener() {
private final String filename = getFilesDir() + "/passwd.txt";
@Override
public void onClick(View view) {
response.setText(textPassword.getText());
if (textPassword.getText().toString().equals("123")) {
Intent myIntent2 = new Intent(MainActivity.this,
Noto.class);
startActivity(myIntent2);
finish();
try {
if (textPassword.getText().toString().equals(readFile(filename).toString())) {
Intent myIntent2 = new Intent(MainActivity.this,
Noto.class);
startActivity(myIntent2);
finish();
}
} catch (IOException e) {
e.printStackTrace();
}
}
});