2017-03-24 40 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