我的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());
这段代码工作正常,但我收到了大量的垃圾字符。我试图寻找它,但无法找到任何相关的东西。任何人都可以帮忙吗?
我还附上了我的输出图片。
答案 0 :(得分:0)
我猜这个文件不是UTF-8。
“任何在8位字符集中无法表示的字符都将作为问号出现。” - > UTF-8
尝试使用UTF-8将应用程序中的内容写入此文件,然后查询它并查看它是否显示正常。