簡體   English   中英

Android讀取/寫入文本文件

[英]Android read / write text files

問題很簡單:我的班上有那些方法的人。 數據保存在一個文本文件中:

//Store the data in a text file
public void saveScores(String testo, TextView text) {

 try {

    FileOutputStream fileout = getActivity().openFileOutput("flagQuiz.txt", getActivity().MODE_PRIVATE);
    OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
    outputWriter.write(testo);
    outputWriter.close();

    text.setText(testo);

 } catch (Exception e) {
    e.printStackTrace();
 }  

}

相反,此方法從文本文件讀取數據。

//Load data from text file
public void loadScores() {

 try {

    FileInputStream fileIn = getActivity().openFileInput("flagQuiz.txt");
    InputStreamReader InputRead= new InputStreamReader(fileIn);

    char[] inputBuffer= new char[100];
    String s="";
    int charRead;

    while ((charRead=InputRead.read(inputBuffer))>0) {

        String readstring=String.copyValueOf(inputBuffer,0,charRead);
        s +=readstring;                 

    }

    InputRead.close();

    } catch (Exception e) {
     e.printStackTrace();
    }

  }

據我所知,第一種方法效果很好,因為它不會引發異常,而且text.setText(testo);不會引發異常text.setText(testo); 工作正常。

問題

應用程序打開(onCreate)時,我無法讀取文本文件。

在此處輸入圖片說明

如您所見,這里有一個空指針異常,這意味着我想文件沒有保存,或者我可能為InputStream輸入了錯誤的路徑。

有什么建議嗎? 我將在內部存儲器上寫這個很小的文本文件。

完整的日志可以在這里找到: link

下面的代碼將從簡歷上的.txt文件讀取數據。

public class MainActivity extends Activity {

    private final static String NOTES = "notes.txt";
    private EditText editText;
    private Button Btn;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            editText = (EditText) findViewById(R.id.editor);
            Btn = (Button) findViewById(R.id.close);

            Btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
        }

        public void onResume(){ //this method will read data from .txt file
            super.onResume();
            try {
                InputStream in = openFileInput(NOTES);
                if (in != null) {
                    InputStreamReader tmp = new InputStreamReader(in);
                    BufferedReader reader = new BufferedReader(tmp);
                    String str;
                    StringBuffer buf = new StringBuffer();

                    while ((str = reader.readLine()) != null) {
                        buf.append(str + "\n");
                    }
                    in.close();
                    editText.setText(buf.toString());
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (Throwable t) {
                Toast.makeText(this, "Exception: " + t.toString(), Toast.LENGTH_LONG).show();
            }

    }

使用OpenFileInput的示例:

    FileInputStream in = openFileInput("filename.txt");
    InputStreamReader inputStreamReader = new InputStreamReader(in);
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = bufferedReader.readLine()) != null) {
        sb.append(line);
    }

暫無
暫無

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

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