ANDROID:如何将JSON数据保存在文件中并进行检索? [英] ANDROID: How to save JSON data in a file and retrieve it?

查看:129
本文介绍了ANDROID:如何将JSON数据保存在文件中并进行检索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android的新手,所以请帮帮我.我试图将ToDoList保存在文件中,以便下次打开它时,所有项目都重新加载

I'm new to android so please help me out. I am trying to save my ToDoList in a file so that the next time I open it, all the items are reloaded

这是我到目前为止的代码,

This is the code I have so far,

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
gson = new Gson();
    try {
        BufferedReader br = new BufferedReader(new FileReader("storage.json"));
        Entry e = gson.fromJson(br, Entry.class);
        Log.d("reading", e.toString());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }}

@Override
protected void onStop() {
    super.onStop();
    json = gson.toJson(mEntries);
    Log.d("jsondata", json);
    try {
        file1 = new FileWriter("storage.json");
        file1.write(json);
        file1.flush();
        file1.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

Entry.java

public class Entry {
String S;
boolean b;

public Entry(String S, boolean b) {
    this.S = S;
    this.b = b;
}

public String getS() {
    return S;
}

public void setS(String S) {
    this.S = S;
}

public void setB(boolean b) {
    this.b = b;
}

public boolean isB() {
    return b;
}

}

如何从这里继续?在onCreate()中,我想检查文件是否存在,如果是,请从文件中导入数据并显示在屏幕上.

How do I proceed from here? In onCreate() I would like to check if the file exists and if yes, import data from file and display on screen.

推荐答案

每个android应用都有自己的内部存储,只有该应用可以访问,您可以从那里读取或写入.

Every android app has its own internal storage only that app can access, you can read from there or write to it.

在这种情况下,您首先要在创建文件之前检查该文件是否存在.

In you case, you first want to check if you such file exist before creating one.

  private String read(Context context, String fileName) {
    try {
        FileInputStream fis = context.openFileInput(fileName);
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader bufferedReader = new BufferedReader(isr);
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            sb.append(line);
        }
        return sb.toString();
    } catch (FileNotFoundException fileNotFound) {
        return null;
    } catch (IOException ioException) {
        return null;
    }
}

private boolean create(Context context, String fileName, String jsonString){
    String FILENAME = "storage.json";
    try {
        FileOutputStream fos = context.openFileOutput(fileName,Context.MODE_PRIVATE);
        if (jsonString != null) {
            fos.write(jsonString.getBytes());
        }
        fos.close();
        return true;
    } catch (FileNotFoundException fileNotFound) {
        return false;
    } catch (IOException ioException) {
        return false;
    }

}

public boolean isFilePresent(Context context, String fileName) {
    String path = context.getFilesDir().getAbsolutePath() + "/" + fileName;
    File file = new File(path);
    return file.exists();
}

活动的onCreate,您可以使用以下操作

onCreate of the Activity, you can use do the following

boolean isFilePresent = isFilePresent(getActivity(), "storage.json");
if(isFilePresent) {
   String jsonString = read(getActivity(), "storage.json");
   //do the json parsing here and do the rest of functionality of app
} else {
   boolean isFileCreated = create(getActivity, "storage.json", "{}");
   if(isFileCreated) {
     //proceed with storing the first todo  or show ui
   } else {
     //show error or try again.
   }
}

参考 https://developer.android.com/guide/topics/data/data-storage.html#filesInternal

这篇关于ANDROID:如何将JSON数据保存在文件中并进行检索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆