简体   繁体   English

Java程序正在csv文件中创建额外的列

[英]Java program is creating extra columns in csv file

I am creating a csv file in a JDBC program that pulls data and then writes it to the csv file. 我正在JDBC程序中创建一个csv文件,该文件提取数据,然后将其写入csv文件。 The problem is that some of the data comes in like "Birmingham, AL" and when the program sees the "," it is creating a new column when really it should all be in one column. 问题是某些数据像“ AL,伯明翰”那样输入,而当程序看到“”时,它实际上是将所有数据都放在一个列中而正在创建一个新列。 The first 15000 rows come in correctly with no commas, but then some commas start appearing and it is creating new columns where it shouldn't. 前15000行正确输入且没有逗号,但是随后出现了一些逗号,并在不应该出现的地方创建了新列。 I was wondering if there was a way to catch and avoid this or to workaround this issue. 我想知道是否有一种方法可以捕获和避免此问题或解决此问题。 I hope I'm explaining this well enough. 我希望我对此解释得足够好。 Feel free to ask for more information. 随时要求更多信息。

EDIT: Here is the snippet of code that does the work. 编辑:这是完成工作的代码片段。

     while (rs.next()) {
            for (int i = 0; i < cols; i++) {
                if (i > 0) {
                    Object value = rs.getObject(i);
                    if (value == null || rs.wasNull())
                        out.write("NULL" + ",");
                    else
                     out.write(value.toString() + ",");
                }
            }
            out.newLine();
        }

        out.close();
        writer.close();

instead of 代替

out.write(value.toString() + ",");

try 尝试

out.write("\""+value.toString() + "\",");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM