简体   繁体   中英

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

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

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}

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

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

You need to extract data from a CSV file according to the format of “fieldname field value”. The code will be rather long if you try to do this in Java.

Try using SPL, the open-source Java package, to get it done. It is easy with only two lines of code:

A
1 =file("data.csv").read@n().select(left(~,2)=="").(right( ,−2).split("")
2 =create(${A1.(~(1)).concat@c()}).record(A1.(~(2)))

SPL offers JDBC driver to be invoked by Java. Just store the above SPL script as csv2tbl.splx and invoke it in Java as you call a stored procedure:

…
Class.forName("com.esproc.jdbc.InternalDriver");
con= DriverManager.getConnection("jdbc:esproc:local://");
st = con.prepareCall("call csv2tbl()");
st.execute();
…

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM