2017-03-24 42 views
-2

嘿这个代码的人我写了一个JSON文件,然后我想读它。但是每次我想要阅读时,我都会回复这样的内容:“[B @ ea6df”。每次都有其他答案。我做错了什么?从JSOn文件获取weired回答

public void writeFile(Context context, String mJsonResponse) { 
    String file_name = "login_datas.json"; 
    try { 
     FileWriter file = new FileWriter(context.getFilesDir().getPath() + "/" + file_name); 
     file.write(mJsonResponse); 
     file.flush(); 
     file.close(); 
    } catch (IOException e) { 
     Log.e("TAG", "Error in Writing: " + e.getLocalizedMessage()); 
    } 
} 

public void readFile(Context context) { 
    try { 
     String file_name = "login_datas.json"; 
     File f = new File(context.getFilesDir().getPath() + "/" + file_name); 
     //check whether file exists 
     FileInputStream is = new FileInputStream(f); 
     int size = is.available(); 
     byte[] buffer = new byte[size]; 
     is.read(buffer); 
     is.close(); 
     Toast.makeText(LoginActivity.this,buffer.toString(),Toast.LENGTH_LONG).show(); 
    } catch (IOException e) { 
     Log.e("TAG", "Error in Reading: " + e.getLocalizedMessage()); 
    } 
} 

回答

1

buffer.toString()不会做你的想法。它不会写入字节数组的内容,而是使用通用的Object.toString()方法,该方法仅以十六进制表示显示对象的类名及其哈希码。

+0

你能给我一个Exemple如何Object.toString()的作品? –

+0

在你的问题中有一个例子:“[B @ ea6df”([B代表一个字节数组,'ea6df'代表哈希代码] Docu:https://docs.oracle.com/javase/8/docs /api/java/lang/Object.html#toString-- – Henry