如何使用bufferedReader捕获csv文件中的字段值 [英] How to capture the field values in the csv file using bufferedreader

查看:91
本文介绍了如何使用bufferedReader捕获csv文件中的字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

csv文件如下所示:

The csv file looks like this

我正在尝试获取诸如name之类的字段值,它是$$ NAME $$之后的值(标识符后面有一个空格).如何使用Java中的BufferedReader存储每个字段的值?字段可以是任何行号,而不是固定的位置或格式,并且如果遇到任何特殊字符或遇到null值,也会引发错误.

I'm trying to get the field values like name, it is the value after $$NAME$$ (there is a space after the identifier). How do I store the value for each field by using BufferedReader in Java? The fields could be in any line number and not in a fixed place or format, and also throw out an error if there is any special characters or null value is encountered.

      int n = 100; // Max lines
              String line;
              try (BufferedReader br = new BufferedReader(new FileReader(str)))
               {
                   while ((line = br.readLine()) != null && i++ < n)
                {
                      br.readLine();
                  line = br.readLine();
                  System.out.println(line);
                 }
               }

从CSV文件中提取值后,我需要将其存储在字符串变量中,并稍后使用它为每个列值插入数据库中

Once the values are extracted from the CSV file, I need to store them in a string variable and use it later to insert into the database for each column values

情况2:对于最后一个字段$$ GROUP $$ CATEGORY,其值也为"5".在9到11单元格中,我需要匹配数据库中的CATEGORY列必须以字符串形式存储在5中,才能插入到同名的数据库列中.当我使用line.matches条件时,正则表达式将找不到确切的匹配项

Case 2:And also for the last field $$GROUP$$ CATEGORY the value is "5" in cell 9 to 11 and i need to match that the column CATEGORY in the database has to be 5 stored in a string to be inserted into the database column of the same name. The regex wont find the exact match when i used line.matches condition

推荐答案

以下代码将仅读取文件的前100行并将值提取到列表中.

The following code will read only the first 100 lines of the file and extract the values into a list.

java.nio.file.Path path = java.nio.file.Paths.get(str);
try {
    java.util.List<String> values = java.nio.file.Files.lines(path)
                                                       .limit(100)
                                                       .filter(line -> line.matches("\\$\\$[A-Z]+\\$\\$ [0-9A-Z]*$"))
                                                       .map(line -> {
                                                           String[] words = line.split(" ");
                                                           return words.length == 2 ? words[1] : "";
                                                       })
                                                       .collect(java.util.stream.Collectors.toList());
    System.out.println(values);
}
catch (java.io.IOException xIo) {
    xIo.printStackTrace();
}

根据您问题中的示例文件,以上代码将创建以下列表.

According to the sample file in your question, the above code will create the following list.

[JOHN, CA, SF, XYZ, , 25, CATEGORY, ]

如果您需要地图而不是 List ,其中 Map 键是两个 $ 字符之间的值,而 Map 值是在空格之后,然后

If you want a Map instead of a List where the Map key is the value between the double $ characters and the Map value is the part after the space, then

Function<String, String> keyMapper = line -> {
    String[] parts = line.split(" ");
    return parts[0].substring(2, parts[0].length() - 2);
};
Function<String, String> valueMapper = line -> {
    String[] parts = line.split(" ");
    if (parts.length > 1) {
        return parts[1];
    }
    else {
        return "";
    }
};
Path path = Paths.get(str);
try {
    Map<String, String> map = Files.lines(path)
                                   .limit(100)
                                   .filter(line -> line.matches("\\$\\$[A-Z]+\\$\\$ [0-9A-Z]*$"))
                                   .collect(Collectors.toMap(keyMapper, valueMapper));
    System.out.println(map);
}
catch (IOException xIo) {
    xIo.printStackTrace();
}

这将创建以下 Map

{GROUP=CATEGORY, WEATHER=, CITY=SF, STATE=CA, TIME=, NAME=JOHN, REGION=XYZ, AGE=25}

这篇关于如何使用bufferedReader捕获csv文件中的字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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