Android读/写文本文件

时间:2015-08-12 12:17:35

标签: android android-studio text storage fileoutputstream

问题很简单:我班上有那些方法。这个保存数据在文本文件中:

//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);工作正常。

问题

应用程序打开时,我无法读取文本文件(onCreate)。

enter image description here

正如你在这里看到的那样,有一个空指针异常,这意味着我猜这个文件是保存的,或者我输入的InputStream路径错误。

有关于此的任何建议吗?我将在内部存储上写下这个非常小的文本文件。

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

2 个答案:

答案 0 :(得分:1)

以下代码将在恢复时从 .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();
            }

    }

答案 1 :(得分:0)

使用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);
    }