使用 bufferedReader 读取文本文件会添加一个空格,android [英] Reading text file with bufferedReader adds a whitespace, android

查看:85
本文介绍了使用 bufferedReader 读取文本文件会添加一个空格,android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个方法可以从文本文件中获取一个 arrayList:

I have this method to get an arrayList from a text file:

private ArrayList<String[]> tempList(){
        String temp = null;
        ArrayList<String[]> tempList = new ArrayList<String[]>();
        try {
            BufferedReader bReader = new BufferedReader(new InputStreamReader(getAssets().open(getFilename())));
            while ((temp = bReader.readLine()) !=null){
            String[] value = temp.split(",");
                tempList.add(value);

            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return tempList;
    }

文件只有用逗号分隔的字母,没有空格或空行.问题是不知何故列表的第一个字母前面有一个空格.所有其他字母都很好.

The file has just letters separated by comma with no space or empty lines. The problem is that somehow the first letter of the list has a whitespace in front of it. All other letters are fine.

这是包含本地字母表的 txt 文件:

here is the txt file which contains a local alphabet:

Α,Β,Γ
Δ,Ε,Ζ
Η,Θ,Ι
Κ,Λ,Μ
Ν,Ξ,Ο
Π,Ρ,Σ
Τ,Υ,Φ
Χ,Ψ,Ω

知道出了什么问题吗?

推荐答案

可能是一个零宽度空间,一个 BOM,'\uFEFF' 通常用作第一个字符(由 Windows 下的记事本)来标记 Unicode 文件.所以 Windows 可以区分 UTF-8 和普通的本地 ANSI.

It could be a zero-width space, a BOM, '\uFEFF' normally used as first char (by NotePad under Windows) to mark Unicode files as such. So Windows can distringuish between UTF-8 and normal local ANSI.

如果在编辑时复制了第一行,然后进行了字符编码转换,那就是解释了.

If during editing the first line was copied, and afterwards a conversion of character encoding took place, that would be the explanation.

temp = temp.replace("\uFEFF", "");

总的来说,移除 BOM 是个好主意.

In general this removal of BOM is a good idea.

这篇关于使用 bufferedReader 读取文本文件会添加一个空格,android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆