How to write / read an array to a file in android

I am writing a file in android and reading from the same file using the code below:

//Write data on file
FileOutputStream fOut = null;

    OutputStreamWriter osw = null;

    try {

        fOut = openFileOutput("gasettings.dat", MODE_PRIVATE);


        osw = new OutputStreamWriter(fOut);

        osw.write(data);

        osw.flush();

        Toast.makeText(context, "Settings saved", Toast.LENGTH_SHORT)
        .show();

    }

    catch (Exception e) {

        e.printStackTrace();


    }

and code to read from a file:

        InputStreamReader isr = null;
fileInputStream fIn = null;

    char[] inputBuffer = new char[255];

    String data = null;

    try {

        fIn = openFileInput("gasettings.dat");

        isr = new InputStreamReader(fIn);

        isr.read(inputBuffer);

        data = new String(inputBuffer);



    }

    catch (Exception e) {

        e.printStackTrace();



    }

as of now, I can only save the line in this file. I like to write a DATE array, and also read data as an array. I know that the return type of the read method will be changed, but I don’t understand how to read and write a DATE array or any other array to a file.

thank

+3
source share
1 answer

In this case, your best bet is to use JSON. This will allow you to save the array in String format, read it and convert it back to the original array.

: http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/11/a-simple-restful-client-at-android/

+1

Source: https://habr.com/ru/post/1751709/


All Articles