Write JSON to a File
<p>I have a class compositionJSON. The class has a method calls makeJSONObject, that creates a JSON-Object and put stuff in it. Here is the code of the class.</p>

<pre><code>public class CompositionJso extends JSONObject {



public JSONObject makeJSONObject (String title, String desc, ArrayList&lt;String&gt; imgPath, ArrayList&lt;Resources&gt; imgView) {

    JSONObject obj = new JSONObject() ;

    try {
        obj.put("title", title);
        obj.put("desc", desc);
        obj.put("imgPath", imgPath);
        obj.put("imgViewPath", imgView);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return obj;
}
</code></pre>

<p>Now I create a instance of this class and call the method in another class. After that I want to write the JSONObject to file and save it on the sd card on device. Here is the code:</p>

<pre><code> saveCompo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setName();
            createJSONFolder();
            CompositionJso obj = new CompositionJso();
            obj.makeJSONObject(compoTitle, compoDesc, imgPaths, imageViewPaths);
            MyCompositionsListActivity.buildList();

            try {
                Writer output = null;
                File file = new File("storage/sdcard/MyIdea/MyCompositions/" + compoTitle + ".json");
                output = new BufferedWriter(new FileWriter(file));
                output.write(obj.toString());
                output.close();
                Toast.makeText(getApplicationContext(), "Composition saved", Toast.LENGTH_LONG).show();

            } catch (Exception e) {
                Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }
            finish();
        }
    });
</code></pre>

<p>The file is saving successfully but if I open it, there is nothing inside. What is wrong with the code?</p>