Write/Read String Array to internal storage android

android, arrays

Solution

To write to a file:

    try {
        File myFile = new File(Environment.getExternalStorageDirectory().getPath()+"/textfile.txt");
        myFile.createNewFile();
        FileOutputStream fOut = new FileOutputStream(myFile);
        OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
        myOutWriter.write("replace this with your string");
        myOutWriter.close(); 
        fOut.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

To read from the file:

    String pathoffile;
    String contents="";

    File myFile = new File(Environment.getExternalStorageDirectory().getPath()+"/textfile.txt");
    if(!myFile.exists()) 
    return "";
    try {
        BufferedReader br = new BufferedReader(new FileReader(myFile));
        int c;
        while ((c = br.read()) != -1) {
            contents=contents+(char)c;
        }

    }
    catch (IOException e) {
        //You'll need to add proper error handling here
        return "";
    }

Thus you will get back your file contents in the string "contents"

Note: you must provide read and write permissions in your manifest file

Problem

I am new to android development. Currently, i am developing a simple app for writing and reading a String Array to an internal storage. First we have A array then save them to storage, then next activity will load them and assign them to array B. Thank you

Original source

Related problems