我需要将List<MyClass> myList
写入CSV文件。特别是我需要写values
。 MyClass具有以下实现:
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;
}
}
代码如下:
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();
}
}
错误消息如下:
java.lang.ClassCastException: myPackage.MyClass cannot be cast to java.lang.CharSequence
如何解决这个问题? THX。
答案 0 :(得分:1)
尝试:
String element = result.next().toString();
writer.append(element);
if(i < columnSize - 1)
writer.append(',');
答案 1 :(得分:1)
你需要在转换为CharSequence之前调用toString()。
答案 2 :(得分:0)
尝试显式调用toString()方法,如
CharSequence element = (CharSequence)result.next().toString();
答案 3 :(得分:0)
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();
}
}
示例:
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();
}
}