我有这个方法从文本文件中获取 arrayList:
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;
}
该文件只有用逗号分隔的字母,没有空格或空行。问题是列表的第一个字母前面有一个空格。所有其他字母都很好。
这是包含本地字母表的 txt 文件:
Α,Β,Γ
Δ,Ε,Ζ
Η,Θ,Ι
Κ,Λ,Μ
Ν,Ξ,Ο
Π,Ρ,Σ
Τ,Υ,Φ
Χ,Ψ,Ω
知道出了什么问题吗?
最佳答案
它可能是一个零宽度空格,一个BOM,'\uFEFF'
通常用作第一个字符(Windows下的NotePad)来标记Unicode文件。因此 Windows 可以区分 UTF-8 和普通的本地 ANSI。
如果在编辑过程中复制了第一行,然后进行了字符编码转换,这就是解释。
temp = temp.replace("\uFEFF", "");
总的来说,删除 BOM 是一个好主意。
关于java - 使用 bufferedReader 读取文本文件会添加空格,android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25263629/