[英]Android-Writing to a file results in random characters
Argargarg。
我試圖從用戶輸入中獲取信息,然后將其寫入系統文件。 我得到輸入,並在其上調用getBytes。 它將類似“ null”和隨機數的行記錄到文件中。 我嘗試將其保存為字符串,但是沒有運氣,它是一個隨機的符號鏈,下面是具體的代碼:
TextView note_input=(TextView) findViewById(R.id.note_input);
FileOutputStream fos=null;
String newNote=note_input.getText().toString();
Log.w("Debug",newNote);
try {
fos=openFileOutput("currentNote",Context.MODE_APPEND);
} catch (FileNotFoundException e) {
//IT_SHOUD_NOT_EXIST
}
try {
Log.w("Debug",newNote.getBytes().toString());
fos.write(newNote.getBytes());
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
感謝您的幫助!
String.getBytes返回字節數組,當您嘗試執行toString()時,實際上是在寫指向字符串的指針。 您已經有String更改此行
Log.w("Debug",newNote.getBytes().toString());
進入
Log.w("Debug",newNote);
並且您將擁有正確的日志輸出,並且文件應該已經正確寫入。
希望這對您有所幫助,並祝您工作愉快
只是在黑暗中拍攝,但我注意到您在調用getBytes()時未指定字符編碼。 除非輸出文件的字符編碼與系統默認編碼的字符編碼相同,否則您很容易在輸出中出現亂碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.