コード例 #1
0
  private File createDelimitedFile(Set<TestBean> theBeans) {

    File aDelimitedFile = null;

    try {

      // Create a temporary file ...
      aDelimitedFile = createTempFile(this.getClass().getName(), ".csv");
      aDelimitedFile.deleteOnExit();

      // Write the set of beans out to the file ...
      BufferedWriter aWriter = new BufferedWriter(new FileWriter(aDelimitedFile));
      for (TestBean aBean : theBeans) {

        StringBuilder aStringBuilder = new StringBuilder();

        aStringBuilder.append(aBean.getFirstName());
        aStringBuilder.append(DEFAULT_DELIMITER);
        aStringBuilder.append(aBean.getLastName());
        aStringBuilder.append(DEFAULT_DELIMITER);
        aStringBuilder.append(aBean.getFavoriteColor());

        aWriter.append(aStringBuilder);
        aWriter.newLine();
      }

      // File all buffers and close up shop ...
      aWriter.flush();
      aWriter.close();

    } catch (IOException e) {

      fail("Failed to create test delimited file.", e);
    }

    return aDelimitedFile;
  }
コード例 #2
0
  @Test
  public void testWriteWithArrayExpansion() {
    StringBuilder longText = new StringBuilder(1000000);
    for (int i = 0; i < 1000000; i++) {
      longText.append(i % 10);
    }

    CsvWriterSettings s = new CsvWriterSettings();
    s.setMaxCharsPerColumn(2);
    CsvWriter w = new CsvWriter(s);

    w.writeValue(longText);
    String value = w.writeValuesToString().trim();
    assertEquals(value.length(), longText.length());
    assertEquals(value, longText.toString());

    w.writeValue(longText);
    w.writeValue(longText);
    value = w.writeValuesToString().trim();
    assertEquals(value.length(), longText.length() * 2 + 1);
    assertEquals(value, longText.toString() + "," + longText.toString());
  }