如何让BufferedReader读取特殊符号

时间:2014-07-09 21:00:34

标签: java csv netbeans utf-8 bufferedreader

我有一个名为'definitions.csv'的文件,其中包含欧洲地区的名称。在每一行上,我都有一个区域名后面的rgbcode。但是,这些区域名称可以包含特殊字符,例如: ñ,é,à...

我已经阅读了一些线程,其中一个解决方案是将BufferedReader(FileReader)更改为 的BufferedReader(InputStreamReader中(的FileInputStream(definition.csv “),” UTF-8" ))

不幸的是,当我打印出Netbeans中的区域名称时,我得到: illustration of faulty netbeansoutput

我将CSV文件中的所有数据提取到一个大字符串中的方法。

    public String retrieveDefinitionsCSVContent(BufferedReader br) {
    try {
// here the file is read ,using the modified Readers
        br = new BufferedReader(new InputStreamReader(new FileInputStream(definition.csv"), "UTF-8"));
        StringBuilder sb = new StringBuilder();
        String lijn = br.readLine();
        while (lijn != null) {
            sb.append(lijn);
            sb.append("\n");
            lijn = br.readLine();
        }
        String DefCSV = sb.toString();
        return DefCSV;
    } catch (FileNotFoundException fnfe) {
        System.out.println("definition.csv not found for readDefinitionCSV()");
    } catch (IOException io) {
        System.out.println("problem occurred reading definition.csv for readDefinitionCSV()");
    }
    return "Error in readDefinitionCSV()";
}

我将前一种方法中的长字符串切割成较小的块的方法,以便我可以更清楚地操作和读取数据。

    public void initBasicRGB(String definitionCSVContent) {
    System.out.println("initBasicRGB");
    String[] lines = definitionCSVContent.split("\n");
    String[] values;
    int i = 0;
    for (String s : lines) {
        values = s.split(";");
        if (!s.isEmpty() && values.length == 6 && i != 0) {
            int red = Integer.parseInt(values[1]);
            int green = Integer.parseInt(values[2]);
            int blue = Integer.parseInt(values[3]);
            String nameProvince = values[4];
// in purgeString I eliminate the special characters by replacing them by their signless counterparts : à=>a (this shouldnt interfere since everything malfunctioned already before I implemented purgeString)
            nameProvince = purgeString(nameProvince);
            basicRGB.put(new Color(red, green, blue), nameProvince);
            System.out.println(new Color(red, green, blue) + "\t" + nameProvince);
            if (isNumber(values[0])) {
                provinceID.put(Integer.parseInt(values[0]), nameProvince);
            }
        }
        i++;
    }
}

1 个答案:

答案 0 :(得分:0)

CSV文件采用iso-8859-1编码,而不是UTF-8。

new BufferedReader (new InputStreamReader(new FileInputStream("C:\\Program Files (x86)\\Steam\\SteamApps\\common\\Crusader Kings II\\common\\landed_titles\\landed_titles.txt"),"iso-8859-1"