Question

I need to write List<MyClass> myList into CSV file. In particular I need to write values. MyClass has the following implementation:

public class MyClass {
    private Object[] values;
//...
    @Override
    public String toString() 
      {
        String line = "";
        for (int i=0; i<this.getSize(); i++) {
            //if (this.values[i] != null) {
                line = line + this.values[i] + " ";
            //}
        }
        return line;
     }
}

The code is the following:

private void saveSolutionToCSV(List<MyClass> solution) {
    int columnSize = solution.get(0).getSize();
    try {
      FileWriter writer = new FileWriter("test.csv");
      Iterator result = solution.iterator();
      while(result.hasNext()) {
         for(int i = 0; i < columnSize; i++) {
            CharSequence element = (CharSequence)result.next(); 
            writer.append(element);
            if(i < columnSize - 1)
             writer.append(',');
         }
         writer.append('\n');
      }
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
}

And the error message is the following:

java.lang.ClassCastException: myPackage.MyClass cannot be cast to java.lang.CharSequence

How to solve this problem? Thx.

Was it helpful?

Solution

Try:

 String element = result.next().toString(); 
 writer.append(element);
 if(i < columnSize - 1)
   writer.append(',');

OTHER TIPS

You need to call toString() before casting to CharSequence.

Try calling the toString() method explicitly like

CharSequence element = (CharSequence)result.next().toString(); 

CsvWriter.java

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Collection;

/**
 * Composes CSV data for collection of objects. Relays on theirs {@link Object#toString toString()} implementation.<br>
 * Could use memory buffer for the data or write straight to the output stream.
 * <p>
 * Example 1:
 * 
 * <pre>
 * Object[][] data = ...
 * 
 * CsvWriter writer = new CsvWriter();
 * writer.write("Column 1", "Column 2", ... "Column N");
 * for (Object[] values : data)
 *      writer.write(values);
 * ...
 * System.out.println(writer.toString());
 * </pre>
 * <p>
 * Example 2:
 * 
 * <pre>
 * Object[][] data = ...
 * 
 * CsvWriter writer = null;
 * try { 
 *      writer = new CsvWriter(new FileOutputStream(new File("data.csv")));
 *      writer.write("Column 1", "Column 2", ... "Column N");
 *      for (Object[] values : data)
 *          writer.write(values);
 * } finally {
 *      writer.close();
 * }
 * </pre>
 * 
 * @author Mykhaylo Adamovych
 */
public class CsvWriter implements Closeable {
    public static final String NULL_MARK = "";
    public static final String QUOTE = "\"";
    private String nullMark = NULL_MARK;
    private final PrintWriter pw;
    private ByteArrayOutputStream baos;

    /**
     * Creates temporary buffer in the memory. <br>
     * Use {@link #toString()} thereafter. No need to close.
     */
    public CsvWriter() {
        baos = new ByteArrayOutputStream();
        pw = new PrintWriter(baos, true);
    }

    /**
     * Doesn't consume memory for CSV data, writes straight to the output stream. Just like {@link FilterOutputStream}, but deal with objects and relays on
     * theirs {@link Object#toString toString()} implementation.<br>
     * Close writer thereafter.
     * 
     * @param os
     *            output stream to write data.
     */
    public CsvWriter(OutputStream os) {
        pw = new PrintWriter(os, true);
    }

    protected String composeRecord(Object... values) {
        if (values == null || values.length == 0)
            return "";
        final StringBuffer csvRecord = new StringBuffer();
        csvRecord.append(QUOTE);
        csvRecord.append(composeValue(values[0]));
        csvRecord.append(QUOTE);
        for (int i = 1; i < values.length; i++) {
            csvRecord.append("," + QUOTE);
            csvRecord.append(composeValue(values[i]));
            csvRecord.append(QUOTE);
        }
        return csvRecord.toString();
    }

    protected String composeValue(Object value) {
        if (value == null)
            return nullMark;
        return value.toString().replaceAll(QUOTE, QUOTE + QUOTE);
    }

    public String getNullMark() {
        return nullMark;
    }

    public void setNullMark(String nullMarker) {
        nullMark = nullMarker;
    }

    @Override
    public String toString() {
        if (baos == null)
            throw new UnsupportedOperationException();
        return baos.toString();
    }

    /**
     * Writes collection of objects as CSV record.
     * 
     * @param values
     */
    public void write(Collection<?> values) {
        write(values.toArray());
    }

    /**
     * Writes collection of objects as CSV record.
     * 
     * @param values
     */
    public void write(Object... values) {
        pw.println(composeRecord(values));
    }

    @Override
    public void close() throws IOException {
        if (baos != null)
            throw new UnsupportedOperationException();
        pw.close();
    }
}

example:

public class MyClass {
    private Object[][] data;
//...
    @Override
    public String toCsv() {
        CsvWriter writer = new CsvWriter();
        csvWriter.write("Column1", "Column2")
        for (final Object[] values : data)
            csvWriter.write(values);
        return writer.toString();
     }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top