Home > Java > javaTutorial > How to Read and Write Strings to Files in Android?

How to Read and Write Strings to Files in Android?

Patricia Arquette
Release: 2024-12-24 19:36:11
Original
963 people have browsed it

How to Read and Write Strings to Files in Android?

Read/Write a String from/to a File in Android

Reading and writing strings to and from files is a fundamental operation in Android development. This article will demonstrate how to accomplish these tasks using the operating system's built-in file I/O methods.

Writing a String to a File

To save a text string to an internal storage file, follow these steps:

  1. Create an OutputStreamWriter object using openFileOutput() and specify the file name and mode.
  2. Write the string to the OutputStreamWriter using write().
  3. Close the OutputStreamWriter.

Reading a String from a File

To retrieve the string from the saved file, follow these steps:

  1. Create an InputStream object using openFileInput().
  2. Create an InputStreamReader object using the InputStream.
  3. Create a BufferedReader object using the InputStreamReader.
  4. Read lines from the file using readLine().
  5. Append the lines to a StringBuilder object.
  6. Close the InputStream.

Example Code

Here's an example code snippet that demonstrates the aforementioned operations:

Writing:

private void writeToFile(String data, Context context) {
    try {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("config.txt", Context.MODE_PRIVATE));
        outputStreamWriter.write(data);
        outputStreamWriter.close();
    } catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());
    }
}
Copy after login

Reading:

private String readFromFile(Context context) {

    String ret = "";

    try {
        InputStream inputStream = context.openFileInput("config.txt");

        if (inputStream != null) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String receiveString = "";
            StringBuilder stringBuilder = new StringBuilder();

            while ((receiveString = bufferedReader.readLine()) != null) {
                stringBuilder.append("\n").append(receiveString);
            }

            inputStream.close();
            ret = stringBuilder.toString();
        }
    } catch (FileNotFoundException e) {
        Log.e("login activity", "File not found: " + e.toString());
    } catch (IOException e) {
        Log.e("login activity", "Can not read file: " + e.toString());
    }

    return ret;
}
Copy after login

The above is the detailed content of How to Read and Write Strings to Files in Android?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template