title: Java export data to (custom-format) text file
body: <p>I was asked to work on this back-end scheduled job that export some customers data (from an e-commerce DB) to a custom-format text file. The code that follows is what I found.</p>
  
  <p>I just would like to delete it all, but I can't. Would it be possible for me to improve this without changing it so much?</p>
  
  <pre><code>public class AConverter implements CustomerConverter {
  
      protected final Logger LOG = LoggerFactory.getLogger(AConverter.class);
  
      private final static String SEPARATOR = ";";
      private final static String CR = "\n";
  
      public String create(Customer customer) {
  
          if (customer == null)
              return null;
  
          LOG.info("Exporting customer, uidpk: {}, userid: {}", customer.getUidPk(), customer.getUserId());
  
          StringBuilder buf = new StringBuilder();
  
          buf.append("&lt;HEAD&gt;");
          buf.append(SEPARATOR);
          buf.append(String.valueOf(customer.getUidPk()));
          buf.append(SEPARATOR);
          byte[] fullName = null;
          try {
              fullName = customer.getFullName().getBytes("UTF-8");
          } catch (UnsupportedEncodingException e1) {
              fullName = customer.getFullName().getBytes();
          }
          String name = null;
          try {
              name = new String(fullName, "UTF-8");
          } catch (UnsupportedEncodingException e) {
              name = customer.getFullName();
          }
          buf.append(limitString(name, 40));
          buf.append(SEPARATOR);
          final CustomerAddress preferredShippingAddress = customer.getPreferredShippingAddress();
          if (preferredShippingAddress != null) {
              final String street1 = preferredShippingAddress.getStreet1();
              if (street1 != null) {
                  buf.append(limitString(street1, 40));
              }
          } else {
              buf.append(" ");
          }
          buf.append(SEPARATOR);
  
          final String addressStr = buildAddressString(customer);
          buf.append(limitString(addressStr, 40));
          buf.append(SEPARATOR);
          buf.append(limitString(customer.getEmail(), 80));
          buf.append(SEPARATOR);
          if (preferredShippingAddress!=null &amp;&amp; preferredShippingAddress.getStreet2() != null) {
              buf.append(limitString(preferredShippingAddress.getStreet2(), 40));
          } else {
              buf.append(" ");
          }
          buf.append(SEPARATOR);
          buf.append(limitString(customer.getPhoneNumber(), 25));
          buf.append(SEPARATOR);
          if (preferredShippingAddress!=null) {
              if(preferredShippingAddress.getCountry()!=null) {
                  buf.append(preferredShippingAddress.getCountry());
              } else {
                  buf.append(" ");
              }
          } else {
              buf.append(" ");
          }
          buf.append(SEPARATOR);
          if (preferredShippingAddress!=null) {
              if(preferredShippingAddress.getCountry()!=null) {
                  buf.append(preferredShippingAddress.getCountry());
              } else {
                  buf.append(" ");
              }
          } else {
              buf.append(" ");
          }
          buf.append(SEPARATOR);
  
          String fodselsnummer = " ";
          try {
              Map&lt;String, AttributeValue&gt; profileValueMap = customer.getProfileValueMap();
              AttributeValue attributeValue = profileValueMap.get("CODE");
              fodselsnummer = attributeValue.getStringValue();
          } catch (Exception e) {
          }
          buf.append(fodselsnummer);
          buf.append(CR);
          final String string = buf.toString();
  
          return string;
  
      }
  
      private String buildAddressString(Customer customer) {
          final CustomerAddress preferredShippingAddress = customer.getPreferredShippingAddress();
          if (preferredShippingAddress != null) {
              final String zipOrPostalCode = preferredShippingAddress.getZipOrPostalCode();
              final String city = preferredShippingAddress.getCity();
              if (zipOrPostalCode != null &amp;&amp; city != null) {
                  return zipOrPostalCode + " " + city;
              } else if(zipOrPostalCode == null &amp;&amp; city != null) {
                  return city;
              } else if(zipOrPostalCode != null &amp;&amp; city == null) {
                  return zipOrPostalCode;
              }
          }
          return " ";
      }
  
      private String limitString(String value, int numOfChars) {
          if (value != null &amp;&amp; value.length() &gt; numOfChars)
              return value.substring(0, numOfChars);
          else
              return value;
      }
  
  }
  </code></pre>