Java StringBuilder到CSV

时间:2015-10-13 12:58:03

标签: java csv stringbuilder

我有一个场景,我需要从多个Web服务中获取一些值,并将所有这些值写为逗号分隔值(CSV)。为实现这一点,我将所有值附加到StringBuilder并使用逗号(,)作为分隔符并打印如下字符串并将其另存为csv:

3,,069529781,,069529781,,viewerRole

但我想为所有这些值添加一些标题。例如:id,id,email,role,然后是下面的值:

action,userId,firstName,lastName

所以我在csv文件中手动添加它们。如果我想动态添加这些标题并将所有值写入CSV文件,该怎么做?请帮我解决这个问题。这是我的代码:

if (usersList.totalCount != 0 && usersList.totalCount >= 1) {
                System.out.println("usersList.totalCount ----->"
                        + usersList.totalCount);
                for (User user : usersList.objects) {
                    if (user != null) {
                        if (user.id != null) {
                            String userRole = getUserRole(user.id);
                            String cnum = getUserUniqueId(user.email);
                            if (user.id != null) {
                                sb.append(user.id);
                            } else {
                                sb.append(",");
                            }
                            String action = "1";

                            if (cnum != null) {
                                if (userRole == null) {
                                    action = "3";
                                }
                            } else {
                                action = "3";
                            }
                            if (cnum != null) {
                                sb.append(",").append(cnum);
                            }

                            if (action != null) {
                                sb1.append(action);
                            }
                            if (cnum != null) {
                                sb1.append(",").append(cnum);
                            } else {
                                sb1.append(",");
                            }

                            if (user.firstName != null) {
                                sb.append(",").append(user.firstName);
                                sb1.append(",").append(user.firstName);
                            } else {
                                sb.append(",");
                                sb1.append(",");
                            }
                            if (user.lastName != null) {
                                sb.append(",").append(user.lastName);
                                sb1.append(",").append(user.lastName);
                            } else {
                                sb.append(",");
                                sb1.append(",");
                            }
                            if (user.screenName != null) {
                                sb1.append(",").append(user.screenName);
                            } else {
                                sb1.append(",");
                            }

                            if (userRole != null) {
                                sb.append(",").append(userRole);
                                // sb1.append(",").append(userRole);
                            } else {
                                sb.append(",");
                                // action = "3";
                                // sb1.append(action);
                            }
                            // sb1.append("1");
                            if (user.email != null) {
                                sb.append(",").append(user.email);
                                sb1.append(",").append(user.email);
                            } else {
                                sb.append(",");
                                sb1.append(",");
                            }

                            if (userRole != null) {
                                sb1.append(",").append(userRole);
                            } else {
                                sb1.append(",");
                            }

                            if (user.partnerData != null) {
                                if (user.partnerData.startsWith("pw")
                                        && user.partnerData.length() == 43) {
                                    sb.append(",");
                                }
                                if (user.partnerData.length() > 43) {
                                    String partnerData = user.partnerData
                                            .substring(44);
                                    sb.append(",").append(partnerData);
                                }
                                if (!user.partnerData.startsWith("pw")) {
                                    sb.append(",").append(user.partnerData);
                                }
                            }
                            sb.append(System.getProperty("line.separator"));
                            sb1.append(System.getProperty("line.separator"));
                        }
                    }
                }
            }

            System.out.println(sb);
            System.out.println(sb1);
        }

0 个答案:

没有答案