2

I have the following .csv file:

Company ABC                             
"Jan 1, 2020 - Sep 30, 2020"                                
Product Country Avg. monthly clients    Avg. month charge   Parts change    Impact  In stock    Clients in list City
Nissan Maxima   USA 6600    0%  -18%    Low 18      
BMW X7 M50i USA 18100   22% 0%  Low 28      
Volvo XC90  USA 880 0%  -12%    Low 10      
Opel Insignia   USA 320 -34%    -34%    Low 23      
Renult Triber   USA 140 -18%    -36%    Low 8       
Toyota Yaris    USA 880 0%  -28%    Low 30      
Ford Mondeo USA 70  -20%    -71%    Low 1       

for delimiter I have empty space(Tab). I tried to use this code in order to read the file using Opencsv:

@Getter
@Setter
public class CsvLine {

    @CsvBindByPosition(position = 1)
    private String model;

    @CsvBindByPosition(position = 2)
    private String country;
}

            String fileName = "C:\\in_progress\\zzz.csv";

            List<CsvLine> beans = new CsvToBeanBuilder(new FileReader(fileName))
                    .withType(CsvLine.class)
                    .withSeparator(' ')
                    .withSkipLines(1)
                    .build()
                    .parse();

            for(CsvLine item: beans){
                System.out.println(item.getModel());
            }

But I get this output:

 X C 9 0 
null
 I n s i g n i a     U S A   3 2 0   - 3 4 %     - 3 4 %     L o w   2 3         
null
 T r i b e r 
null
 Y a r i s   U S A   8 8 0   0 %     - 2 8 %     L o w   3 0         
null
 M o n d e o     U S A   7 0     - 2 0 %     - 7 1 %     L o w   1       
null
null

Do you know how I can the file properly with Java preferably with OpenCSV?

Test file https://www.dropbox.com/s/7jo4i3bs6h8at25/zzz.csv?dl=0

6
  • The header of your csv file looks incorrect - maybe it is causing the issue Commented Nov 9, 2022 at 10:18
  • The output does not match the code you have quoted. Also - the CSV file is invalid, as it is using the separator char (" ") in the field values. What does "for delimiter I have empty space(Tab)" actually mean? Tab and Space are two different things. Commented Nov 9, 2022 at 10:50
  • You should provide your CSV file. Commented Nov 12, 2022 at 7:52
  • I added test file link. Commented Nov 12, 2022 at 10:42
  • The file is encoded in UTF-16, which explains the issue. Commented Nov 12, 2022 at 11:01

1 Answer 1

3
+50

If your CSV file really uses the Tab character as field delimitier, it should be sufficient to change to:

            List<CsvLine> beans = new CsvToBeanBuilder(new FileReader(fileName))
                    .withType(CsvLine.class)
                    .withSeparator('\t')
                    .withSkipLines(2)
                    .build()
                    .parse();

I changed withSeparator argument and increased the number of lines to skip to 2

Sign up to request clarification or add additional context in comments.

4 Comments

The code works but I get a text with empty spaces like this for example: ` O p e l I n s i g n i a ` Any idea what might cause this?
@PeterPenzov Why should we guess? Why don't you provide your file instead?
@Olivier here it is: dropbox.com/s/7jo4i3bs6h8at25/zzz.csv?dl=0 Please try it.
@PeterPenzov The file is encoded in UTF-16.

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.