使用超级CSV解析制表符分隔文件

fel*_*hst -1 java csv supercsv

我正在尝试使用包含两个字段(制表符分隔)的超级CSV和未加引号的字符串来解析CSV文件.

    occugroup_code  occugroup_name
    110000          Management Occupations  
    130000          Business and Financial Operations Occupations   
    150000          Computer and Mathematical Occupations   

我很难弄清楚如何配置CsvPreference以便能够为每两个返回一个Map.有人遇到过这个问题吗?

tim*_*hew 8

请您下次尝试自己做任何事情或者更具体地描述您的问题.
例如:
I try the CsvPreference.xyz but it didn't work, because I get the exception abc


一些基本内容:CSV =以逗号分隔的值
您的文件不是用逗号或分号分隔的,它与选项卡分开.所以你必须创建自己的CsvPreference:

CsvPreference pref = new CsvPreference('\"', '\t', "\n");
Run Code Online (Sandbox Code Playgroud)

这是完整的示例(已测试):

InputStream inputStream = this.getClassLoader().getResourceAsStream("example.csv");
CsvPreference pref = new CsvPreference('\"', '\t', "\n");
ICsvMapReader reader = new CsvMapReader(new InputStreamReader(inputStream), pref);

List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> result;
while ((result = reader.read(new String[]{"code", "name"})) != null) {
    list.add(result);
}

for (Map<String, String> elem : list) {
    System.out.print(elem.get("code")+" | ");
    System.out.print(elem.get("name"));
    System.out.println();
}
Run Code Online (Sandbox Code Playgroud)

输出:

110000 | 管理职业
130000 | 商业和金融业务职业
150000 | 计算机与数学职业