简体   繁体   中英

ArrayIndexOutOfBounds Exception due to line breaks in CSV

I want to upload my csv files to the database. I am getting arrayIndexOutOfBounds Exception due to line breaks in CSV. There are some cells in excel with new line. I have tried all methods by removing linebreaks in excel, replacing \\n with space in code. I am using Putty for writing and running program. When I upload the csv file on putty, those lines come with line breaks even though I used formula to remove linebreaks.(It is showing correctly in Excel now but in putty, it is coming with breaks.)

This is piece of code where I am reading csv:

            try (BufferedReader br = new BufferedReader(new FileReader(csvFile)))
            {
                    int count=0;
                    int lastPersonId;
                    int listId=0;
                    int lineNo=0;
                      while ((line = br.readLine()) != null)
                      {
                            line = line.replace("\n", "").replace("\r", "").trim();

                            if (count==0)
                            {
                                    count++;
                                    continue;
                            }
                            String[] data = line.split(splitBy,-1);

                            prep = DatabaseConfig.dbConnection.prepareStatement(query,Statement.RETURN_GENERATED_KEYS);
                            prep.setString(1,data[1]);
                            prep.setString(2,data[2]);
                            prep.setString(3,data[3]);

How can I solve this issue?

Thanks in advance:)

CSV format provide some form of escaping. If cell value contains line break it wrapped with " . So you should handle this.

I suggest you to use some CSV parsing library.

This problem is solved. While removing line breaks in Excel sheet, I was doing it in wrong way. What I am doing now=

1) In google spreadsheet,press ctrl+F and then more options(three dots)=go to find and replace

2) Enter \\n in find box and select "search using regular expression" checkbox and hit replace all

3) It will replace unnecessary line breaks. CSV file from this Excel worked for me.

Thanks a lot everyone for your valuable suggestions. I will now work on CSVparsers, OpenCSV etc. Thank you:)

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