How to capture the field values in the csv file using bufferedreader

对着背影说爱祢 提交于 2021-01-07 01:25:34

问题


The csv file looks like this

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);
                 }
               }

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

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


回答1:


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, ]

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();
}

This will create the following Map

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



回答2:


You could use regex here to both detect the name line:

int n = 100; // Max lines
String line;
try (BufferedReader br = new BufferedReader(new FileReader(str))) {
    while ((line = br.readLine()) != null && i++ < n) {
        if (line.matches("\\$\\$NAME\\$\|$.$")) {
            System.out.println(line.split(" ")[1]);
        }
    }
}



回答3:


I recommend you first split the line by space, then you'll have something like $$NAME$$, JOHN. Then retrieve the key between the dollar signs.

An example based on your snippet:

int i = 0;
int n = 100; // Max lines
String line;
try (BufferedReader br = new BufferedReader(new FileReader(str))) {
  while ((line = br.readLine()) != null && i++ < n) {
    String[] splitLine = line.split(" ");
    String key = splitLine[0].split("\\$\\$")[1];
    String value = splitLine[1];
    System.out.printf("Name: %s | Value: %s%n", key, value);
  }
}

You could also use a more modern approach using Java 9 Files API and streams, here's an example of that:

  public static void main(String[] args) throws IOException, URISyntaxException {

    Path filePathFromProjectFolder = Path.of(ClassLoader.getSystemResource("file.csv").toURI());
    Map<String, String> csvValues = Files.readAllLines(filePathFromProjectFolder).stream()
        .map(line -> line.split(" "))
        .collect(Collectors.toMap(line -> getKeyName(line[0]), line -> line[1]));

    System.out.println(csvValues);
  }


  private static String getKeyName(String str) {
    return str.split("\\$\\$")[1];
  }


来源:https://stackoverflow.com/questions/65033185/how-to-capture-the-field-values-in-the-csv-file-using-bufferedreader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!