本文整理汇总了Java中com.opencsv.CSVWriter.writeAll方法的典型用法代码示例。如果您正苦于以下问题:Java CSVWriter.writeAll方法的具体用法?Java CSVWriter.writeAll怎么用?Java CSVWriter.writeAll使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.opencsv.CSVWriter
的用法示例。
在下文中一共展示了CSVWriter.writeAll方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generate
import com.opencsv.CSVWriter; //导入方法依赖的package包/类
public static String generate(Map<String, Object> row) {
StringWriter writer = new StringWriter();
CSVWriter csvWriter = new CSVWriter(writer);
String[] keys;
ArrayList<String[]> output = new ArrayList<>();
keys = row.keySet().toArray(new String[] {});
Arrays.sort(keys);
output.add(keys);
String[] values = new String[keys.length];
int i = 0;
for(final String key : keys) {
final Object value = row.get(key);
values[i++] = (value != null) ? value.toString() : "null";
}
output.add(values);
try {
csvWriter.writeAll(output);
return writer.toString();
} catch (Exception e) {
return CeeqlError.errorType(e.getClass().getSimpleName(), e.getMessage());
}
}
示例2: write2File
import com.opencsv.CSVWriter; //导入方法依赖的package包/类
public static void write2File(String filePath,List<String[]> lines) {
//写之前需要先写入header
List<String[]> addHeader = new ArrayList<>();
String[] header = new String[21];
header[0] = "id(编号)";
header[1] = "sysid(系统ID)";
header[2] = "imei(设备IMEI)";
header[3] = "trucknum(车辆编号)";
header[4] = "driverno(司机编号)";
header[5] = "orgcode(机构号)";
header[6] = "type(10为进出区域)";
header[7] = "beginLng(开始经度)";
header[8] = "beginLat(开始纬度)";
header[9] = "beginTime(开始时间)";
header[10] = "endLng(结束经度)";
header[11] = "endLat(结束纬度)";
header[12] = "endTime(结束时间)";
header[13] = "triggerTime(触发时间)";
header[14] = "triggerLng(触发经度)";
header[15] = "triggerLat(触发纬度)";
header[16] = "createTime(创建时间)";
header[17] = "updateTime(更新时间)";
header[18] = "seconds(持续时间|秒)";
header[19] = "additional_info(附加信息)";
header[20] = "additional_key(额外key)";
addHeader.add(header);
addHeader.addAll(lines);
try {
Charset encoder = Charset.forName("GBK");//解决乱码
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(filePath), encoder);
CSVWriter writer = new CSVWriter(out, ',');
writer.writeAll(addHeader);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
示例3: writeMsgHis
import com.opencsv.CSVWriter; //导入方法依赖的package包/类
/**
* 写入(保存)消息历史
*
* @param map key:消息名称 value:消息详情
* @throws IOException
*/
public void writeMsgHis(Map<String, String[]> map) throws IOException {
CSVWriter writer = new CSVWriter(new FileWriter(msgHisFile));
String[] entries = new String[]{"消息名称", "消息类型", "模板ID", "跳转URL", "客服消息类型", "客服消息标题/内容", "客服消息图片URL", "客服消息描述", "客服消息跳转URL"};
writer.writeNext(entries);
writer.writeAll(map.values());
writer.close();
}
示例4: writeTemplateData
import com.opencsv.CSVWriter; //导入方法依赖的package包/类
/**
* 保持模板数据
*
* @param msgName 消息名称
* @throws IOException
*/
public void writeTemplateData(String msgName) throws IOException {
File dir = new File("data/template_data/");
File file = new File("data/template_data/" + msgName + ".csv");
if (!file.exists()) {
dir.mkdirs();
file.createNewFile();
}
CSVWriter writer = new CSVWriter(new FileWriter(file));
List<String[]> records = new ArrayList<String[]>();
// 如果table为空,则初始化
if (MainWindow.mainWindow.getTemplateMsgDataTable().getModel().getRowCount() == 0) {
Init.initTemplateDataTable();
}
// 逐行读取
DefaultTableModel tableModel = (DefaultTableModel) MainWindow.mainWindow.getTemplateMsgDataTable()
.getModel();
int rowCount = tableModel.getRowCount();
String[] arryData;
for (int i = 0; i < rowCount; i++) {
arryData = new String[3];
arryData[0] = (String) tableModel.getValueAt(i, 0);
arryData[1] = (String) tableModel.getValueAt(i, 1);
arryData[2] = ((String) tableModel.getValueAt(i, 2)).trim();
records.add(arryData);
}
// 写入文件
writer.writeAll(records);
writer.close();
}
示例5: save
import com.opencsv.CSVWriter; //导入方法依赖的package包/类
/**
* Saves the table to a file
* @param f the file
* @throws IOException an exception
*/
public void save( File f ) throws IOException
{
CSVWriter csvWriter = new CSVWriter(new FileWriter( f.getAbsoluteFile() ),',');
int N = names.size();
List<String[]> rows = new ArrayList<String[]>( N );
int rowLength = 2 * ndims + 2;
for( int i = 0; i < N; i++ )
{
String[] row = new String[ rowLength ];
row[ 0 ] = names.get( i );
row[ 1 ] = activeList.get( i ).toString();
int k = 2;
int j = 0;
while( j < ndims )
row[ k++ ] = movingPts.get( i )[ j++ ].toString();
j = 0;
while( j < ndims )
row[ k++ ] = targetPts.get( i )[ j++ ].toString();
rows.add( row );
}
csvWriter.writeAll( rows );
csvWriter.close();
}
示例6: episodesToCSV
import com.opencsv.CSVWriter; //导入方法依赖的package包/类
public static void episodesToCSV(final Path containerPath) throws IOException {
//read all episodes
List<Episode> episodes = Episode.readEpisodes(containerPath.resolve("data").toAbsolutePath().toString());
List<String[]> csv = new LinkedList<>();
//use this object to turn the state into a vector of numbers
NumericVariableFeatures features = new NumericVariableFeatures();
State state = episodes.get(0).state(0);
int featuresLength = features.features(state).length;
//write the header: old states, reward, action, new states
String[] header = new String[featuresLength*2+2];
for(int i=0; i<featuresLength; i++)
header[i] = state.variableKeys().get(i).toString();
header[featuresLength] = "reward";
header[featuresLength+1] = "action";
for(int i=0; i<featuresLength; i++)
header[i+featuresLength+2] = "new_"+state.variableKeys().get(i).toString();
csv.add(header);
//for each episode and for each time step
for(Episode e : episodes)
for(int t=0; t<e.maxTimeStep(); t++) {
//write the csv
String[] line = new String[featuresLength*2+2];
double[] preState = features.features(e.state(t));
double[] postState = features.features(e.state(t+1));
for(int i=0; i<featuresLength; i++)
line[i] = String.valueOf(preState[i]);
line[featuresLength] = String.valueOf(e.reward(t+1));
line[featuresLength+1] = String.valueOf(e.action(t));
for(int i=0; i<featuresLength; i++)
line[i+featuresLength+2] = String.valueOf(postState[i]);
csv.add(line);
}
//dump
CSVWriter writer = new CSVWriter(new FileWriter(containerPath.resolve("data.csv").toFile()));
writer.writeAll(csv);
}