3

I am writing to file in android and read from the same file using the below code:

//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 the code for reading from file is:

        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 per now I am only able to save a string to this file. I like to write a DATE array to it and also want to read back the data as array. I know that the return type of read method will be changed, but I am not getting the idea of how to read and write the DATE array or any other array to the file.

Thanks

1 Answer 1

1

In that case, your better choice is using JSON. It will allow you to save an array in String format, read it back and convert it again into the original array.

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

Sign up to request clarification or add additional context in comments.

1 Comment

As dead as Bambi's mother. Just Google: "android json file" and you will get more info than you will be able to read in years.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.