本文整理汇总了Java中com.csvreader.CsvWriter.writeRecord方法的典型用法代码示例。如果您正苦于以下问题:Java CsvWriter.writeRecord方法的具体用法?Java CsvWriter.writeRecord怎么用?Java CsvWriter.writeRecord使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.csvreader.CsvWriter
的用法示例。
在下文中一共展示了CsvWriter.writeRecord方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test121
import com.csvreader.CsvWriter; //导入方法依赖的package包/类
@Test
public void test121() throws Exception {
byte[] buffer;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
CsvWriter writer = new CsvWriter(stream, ',', Charset
.forName("ISO-8859-1"));
writer.writeRecord(new String[] { " 1 ", "2" }, false);
writer.writeRecord(new String[] { " 1 ", "2" });
writer.writeRecord(new String[] { " 1 ", "2" }, true);
writer.writeRecord(new String[0], true);
writer.writeRecord(null, true);
writer.close();
buffer = stream.toByteArray();
stream.close();
String data = Charset.forName("ISO-8859-1").decode(
ByteBuffer.wrap(buffer)).toString();
Assert.assertEquals("1,2\r\n1,2\r\n\" 1 \",2\r\n", data);
}
示例2: createCSVFile
import com.csvreader.CsvWriter; //导入方法依赖的package包/类
/**
* @param string
* @param header
* @return
*/
private void createCSVFile(File file, List<String> header) {
try {
CsvWriter csv = new CsvWriter(new FileWriter(file), ',');
String[] finalHeaders = new String[header.size()];
int pos = 0;
for (String column : header) {
finalHeaders[pos++] = column;
}
csv.writeRecord(finalHeaders);
csv.close();
} catch (IOException e) {
logger.severe(e.getMessage());
System.exit(1);
}
}
示例3: writeAnalysisForConfigurationParams
import com.csvreader.CsvWriter; //导入方法依赖的package包/类
/**
* @param writer
* @param columns
* @throws IOException
*/
private void writeAnalysisForConfigurationParams(CsvWriter writer, int columns)
throws IOException {
String name = "TTH-DTH-BTH";
for (int tth = 0; tth < tths.size(); tth++) {
for (int dth = 0; dth < dths.size(); dth++) {
for (int bth = 0; bth < bths.size(); bth++) {
String[] row = new String[columns];
row[0] = name;
row[1] = this.getStringForPosInTheMatrix(tths, tth) + "-"
+ this.getStringForPosInTheMatrix(dths, dth) + "-"
+ this.getStringForPosInTheMatrix(bths, bth);
row[2] = this.getAvgImpValueForConfigurationsParams(tth, dth, bth);
writer.writeRecord(row);
}
}
}
}
示例4: joinAsCsv
import com.csvreader.CsvWriter; //导入方法依赖的package包/类
/**
* Joins the given values as a comma-separated string. Each value will be encoded in this string by escaping any
* commas in the value with a backslash.
*
* @param values the values to join (null is acceptable).
* @return the CSV string consisting of the values. If the values were null, an empty String.
*/
public static String joinAsCsv(String[] values) {
if (values == null) {
return StringUtils.EMPTY;
}
try {
final StringWriter sw = new StringWriter();
final CsvWriter csvWriter = new CsvWriter(sw, ',');
csvWriter.setEscapeMode(CsvWriter.ESCAPE_MODE_BACKSLASH);
csvWriter.setUseTextQualifier(false);
csvWriter.writeRecord(values);
csvWriter.flush();
csvWriter.close();
return sw.toString();
} catch (final IOException e) {
throw new IllegalStateException("Could not encode as CSV record: " + e, e);
}
}
示例5: writeAuditCount
import com.csvreader.CsvWriter; //导入方法依赖的package包/类
/**
*
* @param csv
* CsvWriter object used to write results
* @param auditCounts
* Audit results
* @param date
* Date
* @param actions
* @throws IOException
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public void writeAuditCount(CsvWriter csv, List<AuditCount> auditCounts, String date, Map<String, Integer> actions) throws IOException {
for (AuditCount auditCount : auditCounts) {
actions.put(auditCount.getTarget(), auditCount.getCount());
}
Iterator i = actions.entrySet().iterator();
while (i.hasNext()) {
Map.Entry<String, Integer> e = (Map.Entry<String, Integer>) i.next();
String[] record = new String[3];
record[0] = date;
record[1] = I18NUtil.getMessage("csv." + (String) e.getKey());
record[2] = ((Integer) e.getValue()).toString();
csv.writeRecord(record);
e.setValue(new Integer(0));
}
}
示例6: writeCsvEntry
import com.csvreader.CsvWriter; //导入方法依赖的package包/类
public void writeCsvEntry(CsvWriter csv, List<CsvExportEntry> csvExportEntries, boolean dateFirst, String date) throws IOException {
for (CsvExportEntry csvExportEntry : csvExportEntries) {
String[] record;
int recordIndex = 0;
if (date != null) {
record = new String[5];
if (dateFirst) {
record[recordIndex++] = date;
}
} else {
record = new String[4];
}
record[recordIndex++] = csvExportEntry.getAuditSite();
record[recordIndex++] = I18NUtil.getMessage("csv." + csvExportEntry.getAuditAppName());
record[recordIndex++] = I18NUtil.getMessage("csv." + csvExportEntry.getAuditAppName() + "."
+ csvExportEntry.getAuditActionName());
record[recordIndex++] = String.valueOf(csvExportEntry.getCount());
if (!dateFirst && date != null) {
record[recordIndex++] = date;
}
csv.writeRecord(record);
}
}
示例7: writeToFile
import com.csvreader.CsvWriter; //导入方法依赖的package包/类
protected void writeToFile(List<ExportEntry> csvEntries) throws IOException {
CsvWriter cwriter = new CsvWriter(fileOutpuStream, VaultCsvEntry.CSV_DELIMITER,
Charset.forName("UTF-8"));
cwriter.writeRecord(((CsvEntry) csvEntries.get(0)).getCsvHeaderRecord());
for (ExportEntry item : csvEntries) {
cwriter.writeRecord(((CsvEntry) item).toCsvRecord());
}
cwriter.flush();
cwriter.close();
}
示例8: writeToFile
import com.csvreader.CsvWriter; //导入方法依赖的package包/类
@Override
protected void writeToFile(List<ExportEntry> csvEntries) throws IOException {
// Setup compression stuff
ZipOutputStream zos = new ZipOutputStream(fileOutpuStream);
zos.setMethod(ZipOutputStream.DEFLATED);
zos.setLevel(9);
// Setup signature stuff
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA-512");
} catch (NoSuchAlgorithmException ex) {
LOG.log(Level.SEVERE, "Missing hash algorithm for signature. No file exported!", ex);
throw new IOException("Missing hash algorithm for signature.");
}
DigestOutputStream dos = new DigestOutputStream(zos, md);
// write data
zos.putNextEntry(new ZipEntry(DATA_ZIP_ENTRY));
CsvWriter cwriter = new CsvWriter(dos, VaultCsvEntry.CSV_DELIMITER,
Charset.forName("UTF-8"));
cwriter.writeRecord(((CsvEntry) csvEntries.get(0)).getCsvHeaderRecord());
for (ExportEntry item : csvEntries) {
cwriter.writeRecord(((CsvEntry) item).toCsvRecord());
}
cwriter.flush();
// add signature file
zos.putNextEntry(new ZipEntry(SIGNATURE_ZIP_ENTRY));
String sigString = (new HexBinaryAdapter()).marshal(md.digest());
zos.write(sigString.getBytes(), 0, sigString.getBytes().length);
// close everything
cwriter.close();
dos.close();
zos.close();
fileOutpuStream.close();
}
示例9: writeToFile
import com.csvreader.CsvWriter; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
protected void writeToFile(final List<ExportEntry> csvEntries) throws IOException {
FileOutputStream fileOutputStream = getFileOutputStream();
CsvWriter cwriter = new CsvWriter(fileOutputStream, VaultCsvEntry.CSV_DELIMITER, Charset.forName("UTF-8"));
cwriter.writeRecord(((CsvEntry) csvEntries.get(0)).getCsvHeaderRecord());
for (ExportEntry item : csvEntries) {
cwriter.writeRecord(((CsvEntry) item).toCsvRecord());
}
cwriter.flush();
cwriter.close();
}
示例10: writeCsvViolations
import com.csvreader.CsvWriter; //导入方法依赖的package包/类
private void writeCsvViolations(String basecase, List<LimitViolation> networkViolations, CsvWriter cvsWriter)
throws IOException {
for (LimitViolation violation : networkViolations) {
String[] values = new String[] {basecase, violation.getSubjectId(), violation.getLimitType().name(),
Float.toString(violation.getValue()), Float.toString(violation.getLimit()) };
cvsWriter.writeRecord(values);
}
cvsWriter.flush();
}
示例11: testResultSet
import com.csvreader.CsvWriter; //导入方法依赖的package包/类
@Test
public void testResultSet() throws IOException {
final int recordCount = 100;
ByteArrayOutputStream out = new ByteArrayOutputStream();
CsvWriter csvWriter = new CsvWriter(new BufferedOutputStream(out),
',', Charset.forName("UTF-8"));
for (int i = 0; i < recordCount; i++) {
csvWriter.writeRecord(new String[]{
"fieldValueA" + i,
"fieldValueB" + i,
"fieldValueC" + i
});
}
csvWriter.close();
CsvReader csvReader = new CsvReader(
new BufferedInputStream(new ByteArrayInputStream(out.toByteArray())),
',', Charset.forName("UTF-8"));
BulkResultSet resultSet = new BulkResultSet(csvReader, Arrays.asList("fieldA", "fieldB", "fieldC"));
int count = 0;
BulkResult result;
while ((result = resultSet.next()) != null) {
assertEquals("fieldValueA" + count, result.getValue("fieldA"));
assertEquals("fieldValueB" + count, result.getValue("fieldB"));
assertEquals("fieldValueC" + count, result.getValue("fieldC"));
count++;
}
assertEquals(recordCount, count);
}
示例12: writeToFile
import com.csvreader.CsvWriter; //导入方法依赖的package包/类
/**
* Writes the data to a CSV file and generates a signature hash.
* Then it puts both of this into a ZIP archive file.
*
* @param csvEntries The {@link ExportEntry} to be exported.
* @throws IOException Thrown if the SHA-512 hash algorithm is missing.
*/
@Override
protected void writeToFile(final List<ExportEntry> csvEntries) throws IOException {
// Setup compression stuff
FileOutputStream fileOutputStream = getFileOutputStream();
final int zipCompressionLevel = 9;
ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
zipOutputStream.setMethod(ZipOutputStream.DEFLATED);
zipOutputStream.setLevel(zipCompressionLevel);
// Setup signature
MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-512");
} catch (NoSuchAlgorithmException exception) {
LOG.log(Level.SEVERE, "Missing hash algorithm for signature. No file exported!", exception);
throw new IOException("Missing hash algorithm for signature.");
}
DigestOutputStream digestOutputStream = new DigestOutputStream(zipOutputStream, digest);
// write data
zipOutputStream.putNextEntry(new ZipEntry(DATA_ZIP_ENTRY));
CsvWriter cwriter = new CsvWriter(digestOutputStream, VaultCsvEntry.CSV_DELIMITER,
Charset.forName("UTF-8"));
cwriter.writeRecord(((CsvEntry) csvEntries.get(0)).getCsvHeaderRecord());
for (ExportEntry item : csvEntries) {
cwriter.writeRecord(((CsvEntry) item).toCsvRecord());
}
cwriter.flush();
// add signature file
zipOutputStream.putNextEntry(new ZipEntry(SIGNATURE_ZIP_ENTRY));
String sigString = (new HexBinaryAdapter()).marshal(digest.digest());
zipOutputStream.write(sigString.getBytes("UTF-8"), 0, sigString.getBytes("UTF-8").length);
// Closing the streams
cwriter.close();
digestOutputStream.close();
zipOutputStream.close();
fileOutputStream.close();
}
示例13: run
import com.csvreader.CsvWriter; //导入方法依赖的package包/类
@Override
public void run(CommandLine line, ToolRunningContext context) throws Exception {
OnlineConfig config = OnlineConfig.load();
OnlineDb onlinedb = config.getOnlineDbFactoryClass().newInstance().create();
String workflowId = line.getOptionValue("workflow");
OnlineWorkflowWcaResults wfWcaResults = onlinedb.getWcaResults(workflowId);
if (wfWcaResults != null) {
if (!wfWcaResults.getContingencies().isEmpty()) {
Table table = new Table(7, BorderStyle.CLASSIC_WIDE);
StringWriter content = new StringWriter();
CsvWriter cvsWriter = new CsvWriter(content, ',');
String[] headers = new String[7];
int i = 0;
table.addCell("Contingency", new CellStyle(CellStyle.HorizontalAlign.center));
headers[i++] = "Contingency";
table.addCell("Cluster 1", new CellStyle(CellStyle.HorizontalAlign.center));
headers[i++] = "Cluster 1";
table.addCell("Cluster 2", new CellStyle(CellStyle.HorizontalAlign.center));
headers[i++] = "Cluster 2";
table.addCell("Cluster 3", new CellStyle(CellStyle.HorizontalAlign.center));
headers[i++] = "Cluster 3";
table.addCell("Cluster 4", new CellStyle(CellStyle.HorizontalAlign.center));
headers[i++] = "Cluster 4";
table.addCell("Undefined", new CellStyle(CellStyle.HorizontalAlign.center));
headers[i++] = "Undefined";
table.addCell("Cause", new CellStyle(CellStyle.HorizontalAlign.center));
headers[i++] = "Cause";
cvsWriter.writeRecord(headers);
for (String contingencyId : wfWcaResults.getContingencies()) {
String[] values = new String[7];
i = 0;
table.addCell(contingencyId);
values[i++] = contingencyId;
int[] clusterIndexes = new int[]{1, 2, 3, 4, -1};
for (int k = 0; k < clusterIndexes.length; k++) {
if (clusterIndexes[k] == wfWcaResults.getClusterIndex(contingencyId)) {
table.addCell("X", new CellStyle(CellStyle.HorizontalAlign.center));
values[i++] = "X";
} else {
table.addCell("-", new CellStyle(CellStyle.HorizontalAlign.center));
values[i++] = "-";
}
}
table.addCell(Objects.toString(wfWcaResults.getCauses(contingencyId), " "), new CellStyle(CellStyle.HorizontalAlign.center));
values[i++] = Objects.toString(wfWcaResults.getCauses(contingencyId), " ");
cvsWriter.writeRecord(values);
}
cvsWriter.flush();
if (line.hasOption("csv")) {
context.getOutputStream().println(content.toString());
} else {
context.getOutputStream().println(table.render());
}
cvsWriter.close();
} else {
context.getOutputStream().println("\nNo results of security rules applications for this workflow");
}
} else {
context.getOutputStream().println("No results for this workflow");
}
onlinedb.close();
}
示例14: run
import com.csvreader.CsvWriter; //导入方法依赖的package包/类
@Override
public void run(CommandLine line, ToolRunningContext context) throws Exception {
OnlineConfig config = OnlineConfig.load();
OnlineDb onlinedb = config.getOnlineDbFactoryClass().newInstance().create();
String workflowId = line.getOptionValue("workflow");
OnlineWorkflowResults wfResults = onlinedb.getResults(workflowId);
if (wfResults != null) {
if (!wfResults.getContingenciesWithActions().isEmpty()) {
Table table = new Table(5, BorderStyle.CLASSIC_WIDE);
StringWriter content = new StringWriter();
CsvWriter cvsWriter = new CsvWriter(content, ',');
String[] headers = new String[5];
int i = 0;
table.addCell("Contingency", new CellStyle(CellStyle.HorizontalAlign.center));
headers[i++] = "Contingency";
table.addCell("State", new CellStyle(CellStyle.HorizontalAlign.center));
headers[i++] = "State";
table.addCell("Actions Found", new CellStyle(CellStyle.HorizontalAlign.center));
headers[i++] = "Actions Found";
table.addCell("Status", new CellStyle(CellStyle.HorizontalAlign.center));
headers[i++] = "Status";
table.addCell("Actions", new CellStyle(CellStyle.HorizontalAlign.center));
headers[i++] = "Actions";
cvsWriter.writeRecord(headers);
for (String contingencyId : wfResults.getContingenciesWithActions()) {
for (Integer stateId : wfResults.getUnsafeStatesWithActions(contingencyId).keySet()) {
String[] values = new String[5];
i = 0;
table.addCell(contingencyId);
values[i++] = contingencyId;
table.addCell(stateId.toString(), new CellStyle(CellStyle.HorizontalAlign.right));
values[i++] = stateId.toString();
table.addCell(Boolean.toString(wfResults.getUnsafeStatesWithActions(contingencyId).get(stateId)), new CellStyle(CellStyle.HorizontalAlign.right));
values[i++] = Boolean.toString(wfResults.getUnsafeStatesWithActions(contingencyId).get(stateId));
table.addCell(wfResults.getStateStatus(contingencyId, stateId).name());
values[i++] = wfResults.getStateStatus(contingencyId, stateId).name();
String json = "-";
if (wfResults.getActionsIds(contingencyId, stateId) != null) {
// json = Utils.actionsToJson(wfResults, contingencyId, stateId);
json = Utils.actionsToJsonExtended(wfResults, contingencyId, stateId);
}
table.addCell(json);
values[i++] = json;
cvsWriter.writeRecord(values);
}
}
cvsWriter.flush();
if (line.hasOption("csv")) {
context.getOutputStream().println(content.toString());
} else {
context.getOutputStream().println(table.render());
}
cvsWriter.close();
} else {
context.getOutputStream().println("\nNo contingencies requiring corrective actions");
}
} else {
context.getOutputStream().println("No results for this workflow");
}
onlinedb.close();
}