我已經看過這裏的其他線程,找不到任何適合我的東西。我需要做的是從外部存儲器讀取文本文件,並將文本複製到我的Java文件中的字符串。在它被轉換後,我需要它將該字符串與用戶在Edittext中輸入的字符串進行比較。這是我迄今爲止所做的,我確信它有很多錯誤。如何將字符串從文本文件複製到Java字符串?
try {
decrypt();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
final EditText pin = (EditText) findViewById(R.id.pin);
pin.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
File file = new File(Environment.getExternalStorageDirectory().toString() + "/Vault/data1.txt");
String pinkey = pin.getText().toString();
if (pinkey.matches("")) {
Toast.makeText(MainActivity.this, "Type in pin", Toast.LENGTH_LONG).show();
}
else {
try {
decrypt();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
StringBuilder text = new StringBuilder();
String key = new String(file.toString());
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
if (file.exists()) {
} else {
showHelp(null);
}
if (pinkey.equals(br)) {
Toast.makeText(MainActivity.this, "You're signed in", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, "Try again", Toast.LENGTH_LONG).show();
}
}
return true;
}
return false;
}
任何幫助表示讚賞!