//your package name
package com.toidiu.app.util;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Type;

public class SaveLoadHandler<T> {

    protected final Type type;
    private String Filename;

    public SaveLoadHandler(Type type, String filename){
        this.type = type;
        this.Filename = filename;
        //Log.d("------TYPE--------",           this.type.toString());
    }

    public void saveData(Context ctx, T data){
        Gson gson = new Gson();
        String write = gson.toJson(data, this.type);
        Log.d("Write", write);


        File file = new File(ctx.getFilesDir() + File.separator + this.Filename);
//Log.d("TEST","WRITING TO FILE: " + file.getAbsolutePath());
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(file, false);

            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(write);
            bufferedWriter.close();
        } catch (IOException e) {
            Log.e("TEST","EXCEPTION WHEN WRITING FILE");
            e.printStackTrace();
        }
    }

    public T loadData(Context ctx) {
        StringBuilder builder = new StringBuilder("");
        File file = new File(ctx.getFilesDir() + File.separator + this.Filename);

        try {
            FileReader fileReader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(fileReader);

            String read;
            while((read = bufferedReader.readLine()) != null){
                builder.append(read);
            }
            bufferedReader.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

        Gson gson = new Gson();
        Log.d("READ", builder.toString());
        return gson.fromJson(builder.toString(), this.type);
    }

}


@@ -373,15 +373,13 @@
   pr
-otec
+iva
 te
-d
  fin
@@ -408,22 +408,16 @@
 ate 
-String Filenam
+File Fil
 e;%0A%0A
@@ -458,22 +458,16 @@
 pe, 
-String filenam
+File fil
 e)%7B%0A
@@ -513,96 +513,17 @@
 File
-name
  = file
-name
 ;%0A
-        //Log.d(%22------TYPE--------%22,           this.type.toString());%0A
     
@@ -554,21 +554,8 @@
 ata(
-Context ctx, 
 T da
@@ -680,153 +680,8 @@
 );%0A%0A
-%0A        File file = new File(ctx.getFilesDir() + File.separator + this.Filename);%0A//Log.d(%22TEST%22,%22WRITING TO FILE: %22 + file.getAbsolutePath());%0A
     
@@ -768,17 +768,17 @@
 eWriter(
-f
+F
 ile, fal
@@ -811,29 +811,17 @@
 Writer b
-ufferedWriter
+w
  = new B
@@ -860,29 +860,17 @@
        b
-ufferedWriter
+w
 .write(w
@@ -889,29 +889,17 @@
        b
-ufferedWriter
+w
 .close()
@@ -1067,19 +1067,8 @@
 ata(
-Context ctx
 ) %7B%0A
@@ -1125,90 +1125,8 @@
 %22%22);
-%0A        File file = new File(ctx.getFilesDir() + File.separator + this.Filename);
 %0A%0A  
@@ -1188,17 +1188,17 @@
 eReader(
-f
+F
 ile);%0A  
@@ -1223,28 +1223,16 @@
 Reader b
-ufferedReade
 r = new 
@@ -1279,20 +1279,20 @@
  String 
-read
+line
 ;%0A      
@@ -1308,28 +1308,16 @@
 le((
-read = bufferedReade
+line = b
 r.re
@@ -1367,20 +1367,20 @@
 .append(
-read
+line
 );%0A     
@@ -1401,28 +1401,16 @@
        b
-ufferedReade
 r.close(
@@ -1487,24 +1487,90 @@
 %0A        %7D%0A%0A
+%0A        if (builder == null)%7B%0A            return null;%0A        %7D%0A
         Gson
@@ -1632,16 +1632,17 @@
 ing());%0A
+%0A
         
@@ -1703,9 +1703,8 @@
    %7D%0A%0A%7D%0A
-%0A

Update SaveLoadHandler.java