Use Java 8 stream to parse CSV file
I have one CSV file, which contains the data of more than 500 companies Each line in the file refers to a specific company dataset I need to parse this file and infer data from each file to call four different web services
. The first line of the CSV file contains the column names I'm trying to write a method that takes string parameters, which is similar to The column headings in the CSV file are related
Based on this parameter, I want this method to parse the file using the stream function of Java 8 and return the data list obtained from the column header of each row / company
I think I make it more complex than it needs, but I can't think of a more effective way to achieve my goal
Any ideas or ideas will be appreciated
Through stack overflow search, I found that the following posts are similar but not identical Parsing a CSV file for a unique row using the new Java 8 Streams API
public static List<String> getData(String titleToSearchFor) throws IOException{ Path path = Paths.get("arbitoryPath"); int titleIndex; String retrievedData = null; List<String> listOfData = null; if(Files.exists(path)){ try(Stream<String> lines = Files.lines(path)){ List<String> columns = lines .findFirst() .map((line) -> Arrays.asList(line.split(","))) .get(); titleIndex = columns.indexOf(titleToSearchFor); List<List<String>> values = lines .skip(1) .map(line -> Arrays.asList(line.split(","))) .filter(list -> list.get(titleIndex) != null) .collect(Collectors.toList()); String[] line = (String[]) values.stream().flatMap(l -> l.stream()).collect(Collectors.collectingAndThen( Collectors.toList(),list -> list.toArray())); String value = line[titleIndex]; if(value != null && value.trim().length() > 0){ retrievedData = value; } listOfData.add(retrievedData); } } return listOfTitles; }
thank you
Solution
You should not reinvent the wheel and use the common CSV parser library For example, you can use Apache commons CSV
It will handle many ideas for you and be more readable There is also opencsv, which is more powerful and has annotation - based data class mapping
try (Reader reader = Files.newBufferedReader(Paths.get("file.csv")); CSVParser csvParser = new CSVParser(reader,CSVFormat.DEFAULT .withFirstRecordAsHeader() ) { for (CSVRecord csvRecord : csvParser) { // Access String name = csvRecord.get("MyColumn"); // (..) }
Editor: anyway, if you really want to do it yourself, please take a look at this example