How to Adding Restore setting feature by FileinputStream

78 Views Asked by At

So I am new in programing and i adding a feature in my app which save all sharedpreference key in value in Device internal data folder by using fileoutputStream like this add all data in map and store in jsonobject

private String maptojson(){
    Map<String, ?> map = prf.getAll();
    JSONObject object = new JSONObject();
    for (Map.Entry<String,?> entry : map.entrySet()){
        try {
            object.put( entry.getKey(), entry.getValue());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return object.toString();
}

Now Use FileOutputStream for write file in internal Storage "data'

public void backupSetting() {
    Throwable th;
    FileOutputStream fileOutputStream;
    IOException e;
    FileOutputStream fileOutputStream2 = null;
    File externalStorageDirectory = Environment.getExternalStorageDirectory();
    if (externalStorageDirectory.exists() && externalStorageDirectory.canWrite()) {
        if (externalStorageDirectory.getUsableSpace() >= 1048576) {
            File file = new File(externalStorageDirectory.toString() + "/data/MyApp/" + "MyAppSetting.ma");
            try {
                new File(file.getParent()).mkdirs();
                file.createNewFile();
                fileOutputStream = new FileOutputStream(file);
                try {
                    fileOutputStream.write(maptojson().getBytes());
                    fileOutputStream.flush();
                    fileOutputStream.close();
                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (IOException e2) {
                            e2.printStackTrace();
                        }
                    }
                } catch (IOException e3) {
                    e = e3;
                    try {
                        e.printStackTrace();
                        if (fileOutputStream != null) {
                        }
                    } catch (Throwable th2) {
                        th = th2;
                        fileOutputStream2 = fileOutputStream;
                        if (fileOutputStream2 != null) {
                            try {
                                fileOutputStream2.close();
                            } catch (IOException e4) {
                                e4.printStackTrace();
                                throw th;
                            }
                        }
                        throw th;
                    }
                }
            } catch (IOException e5) {
                e = e5;
                fileOutputStream = null;
                e.printStackTrace();
                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close();
                    } catch (IOException e6) {
                        e6.printStackTrace();
                    }
                }
            } catch (Throwable th3) {
                th = th3;
                if (fileOutputStream2 != null) {
                }
                try {
                    throw th;
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
            }
        }
    }

Now i want to add restore setting means restore setting by using that file "MyAppSetting.ma". I know i can do it by using FileInputStream but I don't understand how to do? please help if you could

0

There are 0 best solutions below