本文整理匯總了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);
}