Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to write a basic "notepad" app for a school project.

I have created the main class with an editText which I save as String textOutput.

I have used the following to save the string to a file:

FileOutputStream fos = openFileOutput(textOutput, Context.MODE_PRIVATE);
fos.write(textOutput.getBytes());
fos.close();

However Android Developers reference says in order to read I should use the following steps:

To read a file from internal storage:

  • Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream.
  • Read bytes from the file with read().
  • Then close the stream with close().

What does this mean, and how do I implement it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
201 views
Welcome To Ask or Share your Answers For Others

1 Answer

An example of how to use 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);
    }
    inputStreamReader.close();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...