繁体   English   中英

从Android中的SD卡读取文件时出现垃圾字符

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM