2

I wanted to store JSONObject in a file. For the purpose I converted the Object to string and then stored it in a file. The code that I used is :

String card_string =  card_object.toString();
//card_object is the JSONObject that I want to store.

f = new File("/sdcard/myfolder/card3.Xcard");
//file 'f' is the file to which I want to store it.

FileWriter  fw = new FileWriter(f);
fw.write(card_string);
fw.close();

The code works as I want it. Now I want to retrieve that object back from the File. How should I do it? I am getting confused on what to use to read the file ? an InputStream or a FileReader or BufferedReader or what. I am new to JAVA / android development.

please help. A detailed explanation in simple words regarding what I/O functions to be used in different scenarios (like this) is welcome. I have had a look at documentation but your suggestions are welcome.

TheFlash
  • 5,997
  • 4
  • 41
  • 46
neerajDorle
  • 540
  • 7
  • 21
  • 1
    http://stackoverflow.com/questions/10569021/read-json-from-assets-folder-into-an-arraylist-in-android – Nermeen May 10 '13 at 10:42
  • thankyou very much for your answer. can you also have a look at these questions please ? http://stackoverflow.com/questions/16441330/declaring-mime-type-for-a-custom-file-that-is-to-be-sent-via-bluetooth and http://stackoverflow.com/questions/16413498/blue-tooth-file-not-sent-error – neerajDorle May 10 '13 at 12:18

3 Answers3

5

You can use this code to read file.

BufferedReader input = null;
try {
    input = new BufferedReader(new InputStreamReader(
            openFileInput("jsonfile")));
    String line;
    StringBuffer content = new StringBuffer();
    char[] buffer = new char[1024];
    int num;
    while ((num = input.read(buffer)) > 0) {
        content.append(buffer, 0, num);
    }
        JSONObject jsonObject = new JSONObject(content.toString());

}catch (IOException e) {...}

Then you can use your jsonObject:

To get a specific string from JSON Object

String name = jsonObject.getString("name"); 

To get a specific int

int id = jsonObject.getInt("id"); 

To get a specific array

JSONArray jArray = jsonObject.getJSONArray("listMessages"); 

To get the items from the array

JSONObject msg = jArray.getJSONObject(1);
int id_message = msg.getInt("id_message");
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • thankyou very much for your answer sir. Can you please also have a look at these questions to?? http://stackoverflow.com/questions/16441330/declaring-mime-type-for-a-custom-file-that-is-to-be-sent-via-bluetooth and http://stackoverflow.com/questions/16413498/blue-tooth-file-not-sent-error – neerajDorle May 10 '13 at 12:13
4

Use could use a BufferedReader and FileReader:

File file = new File("/sdcard/myfolder/card3.Xcard");
StringBuilder text = new StringBuilder();
BufferedReader br = null;

try {
    br = new BufferedReader(new FileReader(file));
    String line;

    while ((line = br.readLine()) != null) {
        text.append(line);
        text.append('\n');
    }
} catch (IOException e) {
    // do exception handling
} finally {
    try { br.close(); } catch (Exception e) { }
}

In addition I'd propose that you use Environment.getExternalStorageDirectory(); for building up the path. That's less device specific.

Cheers!

Trinimon
  • 13,839
  • 9
  • 44
  • 60
  • thankyou sir very much ! It is working fine.... can you do me one more favour? please have a look at these questions. really need help over it. http://stackoverflow.com/questions/16441330/declaring-mime-type-for-a-custom-file-that-is-to-be-sent-via-bluetooth and http://stackoverflow.com/questions/16413498/blue-tooth-file-not-sent-error – neerajDorle May 10 '13 at 12:11
2
File contentfile = new File(filePath);
@SuppressWarnings("resource")
FileInputStream readJsonTextfile = new FileInputStream(contentfile);

Reader ContetnUrlJson = new InputStreamReader(readJsonTextfile);
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Jithu
  • 1,478
  • 1
  • 13
  • 21
  • thankyou very much for your answer. can you also have a look at these questions please ? http://stackoverflow.com/questions/16441330/declaring-mime-type-for-a-custom-file-that-is-to-be-sent-via-bluetooth and http://stackoverflow.com/questions/16413498/blue-tooth-file-not-sent-error – neerajDorle May 10 '13 at 12:12