2016-10-21 15 views
2

私はandroidに新規ですので、お手伝いください。私はそれを次回開いたときは、すべての項目がアンドロイド:JSONデータをファイルに保存して取得する方法

これは私がこれまで持っているコードですが再ロードされるように、ファイルに

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(); 
    } 
を私のtodolistのを保存しようとしています

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()私はそのファイルが存在するかどうかをチェックしたいと思います。そうなら、ファイルからデータをインポートし、画面に表示します。

+0

可能な複製を行う使用することができます[アンドロイドでファイルから/書き込み文字列を読み取る方法](のhttp:/ /stackoverflow.com/questions/14376807/how-to-read-write-string-from-a-file-in-android) –

+0

[こちら](https://developer.android.com/training/basics/data)をお読みください。 -storage/files.html) –

答えて

1

すべてのアンドロイドアプリには、そのアプリがアクセスできる唯一の独自の内部ストレージがあります。そこから読み取ったり、書き込んだりできます。

この場合、作成する前に、そのようなファイルが存在するかどうかを最初に確認する必要があります。

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 = 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、あなたは以下の

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

関連する問題