2017-11-22 11 views
0

私はアンドロイドデバイスの内部ストレージに空のjsonファイルを持っています。私は オブジェクトと配列を持つ空のjsonファイルを書き込む方法は?

{ 
    "data": [ 

    ] 
} 

がどのように私はこれを行うことができ、 jsonデータのフォーマットを流してそれを書く必要がありますか?

は、私は、私がやりたい次のスニペットを試してみました

BufferedWriter bufferedWriter = null; 
FileWriter fileWriter = null; 

    JSONObject JsonObject = new JSONObject(); 
    JSONArray array = new JSONArray(); 
    try { 
     JsonObject.put("data",array); 
     fileWriter = new FileWriter(file); 
     bufferedWriter = new BufferedWriter(fileWriter); 
     bufferedWriter.write(JsonObject.toString()); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } 

しかし、何もファイルに書き込まれていない、Logcatは

11-22 16:27:43.833 26727-26727/? I/art: Late-enabling -Xcheck:jni 
11-22 16:27:43.849 26727-26735/? E/art: Failed sending reply to debugger: Broken pipe 
11-22 16:27:43.849 26727-26735/? I/art: Debugger is no longer active 
11-22 16:27:43.849 26727-26735/? I/art: Starting a blocking GC Instrumentation 
11-22 16:27:43.942 26727-26727/? W/System: ClassLoader referenced unknown path: /data/app/com.diary.pocket.pocketdiary-2/lib/arm64 
11-22 16:27:43.953 26727-26727/? I/InstantRun: starting instant run server: is main process 
11-22 16:27:43.956 26727-26727/? V/Font: Change font:1 
11-22 16:27:44.003 26727-26727/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 
11-22 16:27:44.105 26727-26745/? I/Adreno: QUALCOMM build     : 853a1ff, I9c435c2712 
             Build Date      : 01/10/17 
             OpenGL ES Shader Compiler Version: XE031.09.00.04 
             Local Branch      : 
             Remote Branch     : 
             Remote Branch     : 
             Reconstruct Branch    : 
11-22 16:27:44.109 26727-26745/? I/OpenGLRenderer: Initialized EGL, version 1.4 
11-22 16:27:44.109 26727-26745/? D/OpenGLRenderer: Swap behavior 1 
11-22 16:27:45.286 26727-26760/com.diary.pocket.pocketdiary D/AppTracker: App Event: start 
11-22 16:27:45.866 26727-26735/? I/art: Ignoring second debugger -- accepting and dropping 
11-22 16:27:46.107 26727-26777/? D/AppTracker: App Event: stop 
11-22 16:27:46.158 26727-26727/? V/BoostFramework: mAcquireFunc method = public int com.qualcomm.qti.Performance.perfLockAcquire(int,int[]) 
11-22 16:27:46.158 26727-26727/? V/BoostFramework: mReleaseFunc method = public int com.qualcomm.qti.Performance.perfLockRelease() 
11-22 16:27:46.158 26727-26727/? V/BoostFramework: mAcquireTouchFunc method = public int com.qualcomm.qti.Performance.perfLockAcquireTouch(android.view.MotionEvent,android.util.DisplayMetrics,int,int[]) 
11-22 16:27:46.158 26727-26727/? V/BoostFramework: mIOPStart method = public int com.qualcomm.qti.Performance.perfIOPrefetchStart(int,java.lang.String) 
11-22 16:27:46.158 26727-26727/? V/BoostFramework: mIOPStop method = public int com.qualcomm.qti.Performance.perfIOPrefetchStop() 
11-22 16:27:46.161 26727-26727/? V/BoostFramework: BoostFramework() : mPerf = [email protected] 
11-22 16:27:46.161 26727-26727/? V/BoostFramework: BoostFramework() : mPerf = [email protected] 
11-22 16:27:46.162 26727-26727/? V/BoostFramework: BoostFramework() : mPerf = [email protected] 
11-22 16:27:46.162 26727-26727/? V/BoostFramework: BoostFramework() : mPerf = [email protected] 
11-22 16:27:46.251 26727-26727/? V/BoostFramework: BoostFramework() : mPerf = [email protected] 
11-22 16:27:46.251 26727-26727/? V/BoostFramework: BoostFramework() : mPerf = [email protected] 
11-22 16:27:46.323 26727-26727/? V/BoostFramework: BoostFramework() : mPerf = [email protected] 
11-22 16:27:46.331 26727-26727/? V/BoostFramework: BoostFramework() : mPerf = [email protected] 
11-22 16:27:46.331 26727-26727/? V/BoostFramework: BoostFramework() : mPerf = [email protected] 
11-22 16:27:46.340 26727-26744/? D/AppTracker: App Event: start 
11-22 16:27:46.475 26727-26727/? W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView 
11-22 16:27:48.941 26727-26727/? V/BoostFramework: BoostFramework() : mPerf = [email protected] 

はこれで私を助けて、同様です。事前に感謝します

+0

あなたを助けるだろう「ファイル」であなたのパスは何ですか?願っていますか – Dhruv

+0

@DhruvPatel 'ファイルファイル=新しいファイル(getActivity().getExternalFilesDir("/appointments ")、" appointments.json ");' – ChyperX

答えて

0

空のファイルに必要な構造を追加したい場合、これを簡単に行うことができます。

try { 
    FileWriter writer = new FileWriter(file); 
    writer.append("{\"data\":[]}"); 
    writer.flush(); 
    writer.close(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

私はこれが