title: Java StringBuilder to CSV
body: <p>I have a scenario where I need to fetch some values from multiple web services and write all those values as a comma separated values (CSV). To achieve this, I am appending all values to StringBuilder with Comma(,) as a Separator and Printing the string as below and saving it as a csv:</p>
  
  <pre><code>3,,069529781,,069529781,,viewerRole
  </code></pre>
  
  <p>But I want to add some headers to all of these values. For ex: id, name, email, role and then values like below:</p>
  
  <pre><code>action,userId,firstName,lastName
  </code></pre>
  
  <p>So I am adding those manually in csv file. If I want to add these headers also dynamically and write all the values to CSV file, How to do that ? Please help me resolving this. Here is my code:</p>
  
  <pre><code>if (usersList.totalCount != 0 &amp;&amp; usersList.totalCount &gt;= 1) {
                  System.out.println("usersList.totalCount -----&gt;"
                          + 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")
                                          &amp;&amp; user.partnerData.length() == 43) {
                                      sb.append(",");
                                  }
                                  if (user.partnerData.length() &gt; 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);
          }
  </code></pre>