2016-07-22 85 views
0

我已經看過這裏的其他線程,找不到任何適合我的東西。我需要做的是從外部存儲器讀取文本文件,並將文本複製到我的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; 
       } 

任何幫助表示讚賞!

回答

0

傻了。我也許應該使用註釋之前滾動條:對

不管怎麼說,試試你的try塊這樣

BufferedReader br = new BufferedReader(new FileReader(file)); 
     try { 
      StringBuilder sb = new StringBuilder(); 
      String line = br.readLine(); 

      while (line != null) { 
       sb.append(line); 
       sb.append(System.lineSeparator()); 
       line = br.readLine(); 
      } 
      String fileAsString = sb.toString(); 
     } finally { 
      br.close(); 
     } 

不知道你是否需要一個新的行分隔符,但如果你比較它與另一個文件時,它可能很好地以代表檔案的形式得到它......我會假設。

0

您可以使用一個簡單的ScannerFile對象:

public String readFile(String path) { 
     try { 
      Scanner se = new Scanner(new File(path)); 
      String txt = ""; 
      while (se.hasNext()) 
       txt += se.nextLine() + "\n"; 
      se.close(); 
      return txt; 
     } catch (FileNotFoundException e) { 
      return null; 
     } 
    } 

您打開掃描文件掃描儀,只要有更多的從文件中讀取,你的下一行追加到串。

如果文件不存在,則此方法返回null,如果文件爲空,則返回空字符串。