簡體   English   中英

使用bufferedReader讀取文本文件會添加一個空格,Android

[英]Reading text file with bufferedReader adds a whitespace, android

我有這種方法從文本文件獲取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, '\'通常用作第一個字符(由Windows下的NotePad標記),以此標記Unicode文件。 因此,Windows可以區分UTF-8和普通的本地ANSI。

如果在編輯過程中復制了第一行,然后進行了字符編碼轉換,那就可以解釋了。

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

通常,刪除BOM是一個好主意。

只需按照@ user3505725的建議對代碼進行一些更改:

String[] value = temp.trim().split(",");

希望這會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM