The simplest form. I don't think it will be used as it is, but can it be used as a base?
The opencsv.jar version is 4.0 and the java version is 1.7.
CsvOutput.java
package com.test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.opencsv.CSVWriter;
public class CsvOutput {
public static void main(String[] args) throws IOException {
CSVWriter csvw = null;
try {
//Instance generation
csvw = new CSVWriter(
new FileWriter(new File("c:\\test", "test.csv"))
, ",".charAt(0)
, "\"".charAt(0)
, "\"".charAt(0)
, "\r\n");
//An array of String becomes one row of data
List<String[]> outStrList = new ArrayList<String[]>();
//Define the number of items for the number of arrays
//Actually, I don't think I'll write it like this ...
String[] outStr = new String[2];
outStr[0] = "Drinking too much";
outStr[1] = "Spit out";
outStrList.add(outStr);
//writing
csvw.writeAll(outStrList);
} catch (Exception e) {
e.printStackTrace();
} finally {
//Close at the end
if (csvw != null) {
csvw.close();
}
}
}
}
・ Output result
test.csv
-Output file contents
"Drinking too much","Spit out"
Recommended Posts