mardi 19 avril 2016

Avoiding comma at the start of .CSV file in java

I am scrapping data from a website and store it in CSV file. When the data gets in the CSV file it was getting the comma at the last place of every line. Somehow I manage to handle it. But, now I am getting that comma at the very start of every line which is creating another column. Following is my code.

for (Iterator<Element> it = tdElements.iterator(); it.hasNext();) {
                    if (it.hasNext()) {
                        sb.append("  \n  ");
                    }
                    for (Iterator<Element> it2 = trElement2.iterator(); it.hasNext();) {
                        Element tdElement = it.next();
                        final String content = tdElement.text();

                        if (it2.hasNext()) {
                            sb.append("   ,   ");
                            sb.append(formatData(content));
                        }

                        if (!it2.hasNext()) {
                            String content1 = content.replaceAll(",$", " ");
                            sb.append(formatData(content1));
                            break;
                        } //to remove last placed Commas.

                    }

                    System.out.println(sb.toString());
                    sb.flush();
                    sb.close();




Aucun commentaire:

Enregistrer un commentaire