Search code examples
androidarrayspersist

Android - How to save an array on internal storage


my question is about the following.

Im just trying to generate a new txt file in my internal storage. Inside it i will have just an array converted to string to save some IDs that i need to persist.

After that i need to read that file too but i don't know how to start.

I think is something like this:

   private void GsonWriter(ArrayList<Integer> arrayListTouched){

    String str = arrayListTouched.toString();
    Gson gson = new GsonBuilder().create();

Solution

  • Since you're already using Gson.

    How to save it.

    Gson gson = new GsonBuilder().create();
    ArrayList<Integer> array = new ArrayList<>(); // Integer in your case. But can be any type
    
    // save this string
    String arrayString = gson.toJson(array);
    
    

    How to load it.

    ArrayList<Integer> array = gson.fromJson(arrayString, new TypeToken<ArrayList<Integer>>(){}.getType());
    

    I assume you know how to save/load the string