简体   繁体   中英

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();

Result which I want eg: a,b,c,d,e
Result which I am getting eg: ,a,b,c,d,e

If you're developing in Java 8, I suggest that you use StringJoiner . With this new class, you don't have to build the string yourself. You can find an example to create a CSV with StringJoiner here .

I hope it helps.

StringBuffer sb = new StringBuffer(" ");
    for (Iterator<Element> it = tdElements.iterator(); it.hasNext();) {
                    if (it.hasNext()) {
                        sb.deleteCharAt(sb.length() - 1);
                        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(formatData(content));
                            sb.append(",");

                        }

                        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();


}

im trying to remove the last character which in your case is a , at the instance where it is trying to move to a new line try replacing with my code and make sure to instantiate stringbuffer with a space passed as a string.

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