2013-05-10 64 views
1

我想将JSONObject存储在文件中。 出于此目的,我将Object转换为字符串,然后将其存储在一个文件中。 我使用的代码是:从文件中检索JSONObject

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(); 

该代码正常工作,因为我需要它。现在我想从文件中取回那个对象。 我该怎么做?我对使用什么来读取文件感到困惑? 和InputStreamFileReaderBufferedReader或什么。我是JAVA/android开发新手。

请大家帮忙。用简单的语言详细解释在不同情况下(如这样)使用哪些I/O函数是值得欢迎的。我看过文档,但您的建议是受欢迎的。

+1

http://stackoverflow.com/questions/10569021/read-json-from-assets-folder-into-an-arraylist-in-android – Nermeen 2013-05-10 10:42:56

+0

三江源拿到物品非常适合你的答案。你还可以看看这些问题吗? http://stackoverflow.com/questions/16441330/declaring-mime-type-for-a-custom-file-that-is-to-be-sent-via-bluetooth和http://stackoverflow.com/questions/ 16413498/blue-tooth-file-not-sent-error – neerajDorle 2013-05-10 12:18:04

回答

3

使用可以使用BufferedReaderFileReader

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) { } 
} 

另外我建议你使用Environment.getExternalStorageDirectory();建立起来的路径。这是不太具体的设备。

干杯!

+0

谢谢你,先生,非常感谢!它工作正常....你能再帮我一次吗?请看看这些问题。真的需要帮助。 http://stackoverflow.com/questions/16441330/declaring-mime-type-for-a-custom-file-that-is-to-be-sent-via-bluetooth和http://stackoverflow.com/questions/ 16413498/blue-tooth-file-not-sent-error – neerajDorle 2013-05-10 12:11:05

1
File contentfile = new File(filePath); 
@SuppressWarnings("resource") 
FileInputStream readJsonTextfile = new FileInputStream(contentfile); 

Reader ContetnUrlJson = new InputStreamReader(readJsonTextfile); 
+0

非常感谢您的回答。你还可以看看这些问题吗? http://stackoverflow.com/questions/16441330/declaring-mime-type-for-a-custom-file-that-is-to-be-sent-via-bluetooth和http://stackoverflow.com/questions/ 16413498/blue-tooth-file-not-sent-error – neerajDorle 2013-05-10 12:12:12

4

您可以使用此代码读取文件。

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) {...} 

然后你可以使用你的JSONObject:

要想从JSON对象的特定字符串

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

获取特定INT

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

为了得到一个特定的阵列

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

从数组

JSONObject msg = jArray.getJSONObject(1); 
int id_message = msg.getInt("id_message"); 
+0

谢谢你的回答,先生。你能否也请看看这些问题? http://stackoverflow.com/questions/16441330/declaring-mime-type-for-a-custom-file-that-is-to-be-sent-via-bluetooth和http://stackoverflow.com/questions/ 16413498 /蓝牙文件 - 不发送错误 – neerajDorle 2013-05-10 12:13:01