-4

I'm trying to collect CSV row as array of strings using simpleflatmapper:

try (Reader in = Files.newBufferedReader("path")) {
    return org.simpleflatmapper.csv.CsvParser
//  .mapTo(String[].class)
    .stream(in)
//  .parallel()
//  .flatMap(Arrays::stream)
    .map(line -> {return new ArrayList<>(Arrays.asList(line));})
//  .map(Arrays::asList)
    .collect(Collectors.toList());
} catch (Exception e) {
    e.printStackTrace();
}

As I debug, the line is String[] but the value is entire row (one element) instead of many strings (many cells). How can I got the array of cells?

The CSV file is no special. Ex:
a\t b\t 1\t 2
x\t y\t 3\t 4

The issue as I see in this code .map(line -> {return new ArrayList<>(Arrays.asList(line));}) that the line contains one string value that is the whole line (with tab, space, ...) instead of many strings (each string is the value of each cell).

The whole result I want is List<List<String>> (List of lines). Each line is List<String> (list of cells). The current result is list of lines (rows), each line/row is the whole string.

8
  • What CSV parser are you using? Web search seems to only find CSVParser classes. No CsvParser. Since your question is about the Stream generated by CsvParser, how do you expect us to help, when we have no clue what it is? Commented Oct 16, 2017 at 5:00
  • I'm using simpleflatmapper. Commented Oct 16, 2017 at 6:27
  • I have written that I'm using simpleflatmapper but there are three down votes because of this. Commented Oct 16, 2017 at 6:29
  • 1
    Because few on here know of simpleflatmapper, and you didn't link to it or show import statement. "simpleflatmapper" sounds like something related to the Stream flatMap method, which threw us all for a loop. Also, you haven't shown us the input, so how would we know what you're expecting to see. Perhaps if you provided a Minimal, Complete, and Verifiable example, you wouldn't get down-votes. Commented Oct 16, 2017 at 6:36
  • Possible duplicate of Java accessing specific element in array of strings Commented Oct 16, 2017 at 6:40

2 Answers 2

0

since the file is CSV, you don't need to use any external lib, so simply you have to read the file as you read a txt file like this

Scanner scanner=new Scanner(new File("MyFile.csv"));
while(scanner.hasNextLine()){
myArray=scanner.nextLine().split(",");
}
Sign up to request clarification or add additional context in comments.

4 Comments

We must use a library here.
No you don't to use any external lib unless if that the requirment
Yes, that is the requirement.
This will only work if the CSV has no quoted fields with delimiters and line breaks inside. Also, it will be way slower than using a proper library.
0

I have found the solution:

return org.simpleflatmapper.csv.CsvParser
                    .separator('\t')    //<-- solution
                    .stream(in)
                    .map(Arrays::asList)
                    .collect(Collectors.toList());

Thanks all!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.