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

How to Read and Write Strings to a File in Android?

Susan Sarandon
Release: 2024-12-20 20:41:11
Original
158 people have browsed it

How to Read and Write Strings to a File in Android?

Read and Write String from a File in Android

This article addresses the task of storing user input from an EditText field in a file on the internal storage of an Android device. Additionally, it explores how to retrieve the stored text from the file and assign it to a string variable for subsequent use.

Saving User Input to a File

To save the user input from the EditText with the ID "ID" to the internal memory, the writeToFile method can be used:

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

This method takes two parameters: the data to be written to the file and the context of the activity. It opens a file named "config.txt" in write mode, writes the data into the file, and closes the file.

Reading Input from File

To retrieve the stored text from the "config.txt" file and assign it to the string variable myID, the readFromFile method can be used:

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

This method opens the specified input file, reads line by line, and appends the result to a string builder. The resulting string is returned and can be assigned to myID for later use.

By incorporating these methods into your code, you can effectively store and retrieve user input from an Android device's internal storage, enabling you to build more comprehensive and user-friendly applications.

The above is the detailed content of How to Read and Write Strings to a File 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