0

I am having an issue when reading from a file using comma split. I can read the file like this:

CSVReader reader = new CSVReader(new FileReader(FileName), '|' , '"' , 0);

Then when I want to get the individual values, I can read them like this:

String[] record = rowString.split(","); 

The issue of course is that comma is not the most reliable way to read a file. Is there any way to split the string by pipe delimited like this?:

String[] record = rowString.split("\\|"); 

This is how I am reading the lines, it may possibly be in this code where I need to make such adjustment?

 for(String[] row : allRows){ 
   String rowString = Arrays.toString(row).toString(); 
   String[] record = rowString.split(","); 
 }

Thank you.

Jesse Hernandez
  • 307
  • 2
  • 15
  • String[] record = rowString.split("\\|"); should work fine. But again, CSV files are commonly used for delimiting one-word strings. If your individual strings are multi-word, it makes sense to use a delimiter like a pipe to account for actual commas in the them. – Piyush May 18 '17 at 01:07
  • What is the issue that you are facing? any stack trace? – Rao Jul 02 '17 at 04:51

1 Answers1

0

I don't know if this answer the question but in my case this solve the problem:

val reader: Reader = Files.newBufferedReader(path)

    val csvToBean = CsvToBeanBuilder<MyCsvSchema>(reader)
        .withType(MyCsvSchema::class.java)
        .withSeparator('|')
        .withIgnoreLeadingWhiteSpace(true)
        .build()

    val list = csvToBean.parse()

This is a Kotlin code

arthurfnsc
  • 915
  • 3
  • 13
  • 30
  • are you sure you set the separator on `CsvToBean`? In my case, it only worked when setting the separator on `CSVParser` (`new CSVParserBuilder().withSeparator('|')`) – Krusty the Clown Jul 10 '23 at 19:46