[英]Junk characters when file is read from SD Card in android
我的sdcard中有一個名為file.txt的文件,其中包含文本“ hello android”。 我正在嘗試使用下面給出的代碼讀取此文件
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, "file.txt");
StringBuilder builder = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
String line;
while ((line = br.readLine()) != null) {
builder.append(line);
builder.append('\n');
}
br.close();
}
catch (IOException e) {
Log.d(TAG, "onClick: exception : " + e.getMessage());
}
Log.d(TAG, "onClick: text : " + builder.toString());
這段代碼可以正常工作,但是文本中出現了很多垃圾字符。 我試圖尋找它,但是找不到任何相關的東西。 有人可以幫忙嗎?
我還附上我的輸出圖片。
我猜該文件不是UTF-8。
“任何在8位字符集中無法表示的字符都將作為問號出現。” -> UTF-8
嘗試使用UTF-8將應用程序中的內容寫入此文件,然后再查詢回來,看看是否顯示正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.