簡體   English   中英

如何將字符串從文本文件復制到Java字符串?

[英]How do I copy string from text file to Java string?

我瀏覽了這里的其他線程,找不到對我有用的任何東西。 我需要做的是從外部存儲中讀取一個文本文件,然后將文本復制到Java文件中的字符串中。 轉換后,我需要它將該字符串與用戶在Edittext中鍵入的字符串進行比較。 這是我到目前為止所擁有的,並且我敢肯定這有很多錯誤。

            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;
                }

任何幫助表示贊賞!

傻我 我可能應該在評論之前使用滾動條:p

無論如何,嘗試這樣的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();
        }

不知道是否需要一個新的行分隔符,但是如果將其與另一個文件進行比較,最好以代表文件的形式獲取它...我想。

您可以使用簡單的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如果文件為null則返回空字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM