Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

java - Android: Reading from file (Openfileinput)

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

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

1 Reply

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...