本文整理汇总了Java中com.opencsv.CSVWriter.flush方法的典型用法代码示例。如果您正苦于以下问题:Java CSVWriter.flush方法的具体用法?Java CSVWriter.flush怎么用?Java CSVWriter.flush使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.opencsv.CSVWriter
的用法示例。
在下文中一共展示了CSVWriter.flush方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: processCsv
import com.opencsv.CSVWriter; //导入方法依赖的package包/类
private String processCsv(TableModel model, Writer dest) throws IOException {
CSVWriter writer = new CSVWriter(dest, DELIMITER, QUOTE);
try {
String[] row = new String[model.getColumnCount()];
for (int j = 0; j < model.getColumnCount(); j++)
row[j] = model.getColumnName(j);
writer.writeNext(row);
for (int i = 0; i < model.getRowCount(); i++) {
for (int j = 0; j < model.getColumnCount(); j++)
row[j] = (String) model.getValueAt(i, j);
writer.writeNext(row);
}
writer.flush();
return (String.format("Exported %d rows", model.getRowCount()));
} finally {
writer.close();
}
}
示例2: parsePaboDownload
import com.opencsv.CSVWriter; //导入方法依赖的package包/类
/**
* Parses a Pabo export csv File with a list of students.
* Each student in this list is not null,
* has a valid matriculation number, and has a valid pabo grade.
* @param students The list of students which pabo export should occur. The students
* must all have valid pabo data and a valid pabo grade.
* @param examDate The examDate of the course. If it's null, an empty string gets
* written for the examDate.
* @param fileName The fileName of the CSV file with the .csv ending.
* @param dirPath The path to the directory to which the file should be written.
* @see PaboData
* @see PaboGrade
* @return The csv file of the Pabo export.
*/
public static File parsePaboDownload(List<Student> students, Date examDate,
String fileName, String dirPath) throws IOException {
String examDateStr = examDate == null ? ""
: new SimpleDateFormat("dd.MM.yyyy", Locale.GERMANY)
.format(examDate);
log.debug(String.format("parsePaboDownload() called with exam date %s,"
+ " fileName %s and path %s", examDateStr, fileName, dirPath));
if (students == null || dirPath == null || fileName == null) {
throw new IllegalArgumentException("The students, fileName and dirPath cant be null");
}
File csv = new File(dirPath, fileName);
log.debug("Got file: " + csv.toPath());
log.debug("Opening CSVWriter");
CSVWriter writer = new CSVWriter(new OutputStreamWriter(
new FileOutputStream(csv), Charsets.ISO_8859_1), SEPARATOR, QUOTE_CHAR);
log.debug("Writing headline");
writer.writeNext(PABO_CSV_FILE_HEADER);
for (Student student: students) {
writer.writeNext(parseStudentToRow(student, examDateStr));
}
log.debug("Trying to flush");
writer.flush();
log.debug("Trying to close");
writer.close();
return csv;
}
示例3: parseCourseToCSV
import com.opencsv.CSVWriter; //导入方法依赖的package包/类
/**
* Parses the data of a course (the students, gradings, tutorials etc.) into
* a csv file.
* The CSV File has the matriculation number, the tutorial, the group and
* every grading of the student. If the user is not a lecturer, only the data
* the privileged user has access to (gradings, tutorial, groups) get displayed.
* If the data is not available, an empty string gets placed in the data.
* If the user has no access to the data, a question mark ('?') gets displayed
* instead of the data.
* The data is in .csv file format.
* @param course the course from which the data should be parsed.
* @param isLecturer Whether the user is a lecturer.
* @param privUser The privileged user when the user is not a lecturer. Should
* be null if the user is lecturer, shouldn't be when he is not.
* @throws IllegalArgumentException If the course, dirPath, or fileName is null.
* If the user is not a lecturer but the privUser
* is null.
* @throws IOException If the parsing the file goes wrong.
*
*/
public static File parseCourseToCSV(Course course, boolean isLecturer,
PrivilegedUser privUser, String dirPath,
String fileName) throws IOException {
if (dirPath == null || fileName == null || course == null) {
throw new IllegalArgumentException("The course, dir path and file name cant "
+ " be null.");
}
if (!isLecturer && privUser == null) {
throw new IllegalArgumentException("When the user is not a lecturer "
+ " the priv user cant be null.");
}
File csv = new File(dirPath, fileName);
log.debug("Ceating the csv writer");
CSVWriter writer = new CSVWriter(new OutputStreamWriter(
new FileOutputStream(csv), Charsets.UTF_8),
SEPARATOR, QUOTE_CHAR);
log.debug("Writing header line");
writer.writeNext(getHeader(course));
log.debug("Writing the data of students");
for (Student student: course.getStudents()) {
log.debug("Writing for " + student.getUser());
writer.writeNext(getStudentDataRow(student, isLecturer, privUser));
}
log.debug("Trying to flush");
writer.flush();
log.debug("Trying to close");
writer.close();
return csv;
}
示例4: getSummaryTimingCsv
import com.opencsv.CSVWriter; //导入方法依赖的package包/类
/**
* @{inheritDoc
*/
@Override
public Response getSummaryTimingCsv(final String jobId) {
ResponseBuilder responseBuilder = Response.ok();
// AuthUtil.checkLoggedIn(servletContext);
StreamingOutput streamingOutput = new StreamingOutput() {
@Override
public void write(OutputStream output) throws IOException, WebApplicationException {
List<SummaryData> data = new SummaryDataDao().findByJobId(Integer.parseInt(jobId));
if (!data.isEmpty()) {
CSVWriter csvWriter = new CSVWriter(new OutputStreamWriter(output));
try {
String[] headers = ReportUtil.getSummaryHeaders();
csvWriter.writeNext(headers);
for (SummaryData item : data) {
String[] line = getLine(item);
csvWriter.writeNext(line);
csvWriter.flush();
}
} catch (RuntimeException e) {
throw e;
} finally {
csvWriter.close();
}
}
}
};
String filename = "timing_" + new TankConfig().getInstanceName() + "_" + jobId + ".csv";
responseBuilder.header("Content-Disposition", "attachment; filename=\"" + filename + "\"");
responseBuilder.entity(streamingOutput);
return responseBuilder.build();
}
示例5: getTimingBucketCsv
import com.opencsv.CSVWriter; //导入方法依赖的package包/类
/**
* @{inheritDoc
*/
@Override
public Response getTimingBucketCsv(final String jobId, final int period, final String min, final String max) {
ResponseBuilder responseBuilder = Response.ok();
final String tableName = ReportUtil.getBucketedTableName(jobId);
StreamingOutput streamingOutput = new StreamingOutput() {
@Override
public void write(OutputStream output) throws IOException, WebApplicationException {
Date minDate = null;
Date maxDate = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
minDate = StringUtils.isBlank(min) ? null : sdf.parse(min);
maxDate = StringUtils.isBlank(max) ? null : sdf.parse(max);
} catch (ParseException e1) {
LOG.warn("Could not parse date : " + e1);
}
PeriodicDataDao dao = new PeriodicDataDao();
List<PeriodicData> data = dao.findByJobId(Integer.parseInt(jobId), minDate, maxDate);
LOG.info("found " + data.size() + " entries for job " + jobId + " for dates " + minDate + " - " + maxDate);
if (!data.isEmpty()) {
CSVWriter csvWriter = new CSVWriter(new OutputStreamWriter(output));
try {
String[] headers = ReportUtil.BUCKET_HEADERS;
csvWriter.writeNext(headers);
int count = 0;
if (period > 15) {
data = consolidatePeriod(data, period);
}
for (PeriodicData item : data) {
count++;
String[] line = getBucketLine(jobId, period, item);
csvWriter.writeNext(line);
if (count % 500 == 0) {
csvWriter.flush();
}
}
csvWriter.flush();
} catch (RuntimeException e) {
throw e;
} finally {
csvWriter.close();
}
}
}
};
String filename = tableName + "_" + jobId + ".csv";
responseBuilder.header("Content-Disposition", "attachment; filename=\"" + filename + "\"");
responseBuilder.entity(streamingOutput);
return responseBuilder.build();
}
示例6: convert
import com.opencsv.CSVWriter; //导入方法依赖的package包/类
public void convert(InputStream in, OutputStream out) throws IOException {
Date start = new Date();
SasFileReader reader = new SasFileReaderImpl(in);
CSVWriter writer = new CSVWriter(new OutputStreamWriter(out));
Object[] data;
SasFileProperties properties = reader.getSasFileProperties();
log.info("Reading file " + properties.getName());
log.info(properties.getRowCount() + " rows.");
List<Column> columns = reader.getColumns();
String[] outData = new String[columns.size()];
// Writing column labels
for(int i=0; i < columns.size(); i++) {
outData[i] = columns.get(i).getLabel();
}
writer.writeNext(outData);
// Writing column names
for(int i=0; i < columns.size(); i++) {
outData[i] = columns.get(i).getName();
}
writer.writeNext(outData);
// Writing column format
for(int i=0; i < columns.size(); i++) {
outData[i] = columns.get(i).getFormat();
}
writer.writeNext(outData);
try {
log.info("Writing data...");
long rowCount = 0;
while((data = reader.readNext()) != null) {
assert(columns.size() == data.length);
for(int i=0; i < data.length; i++) {
outData[i] = data[i] == null ? "" : data[i].toString();
}
writer.writeNext(outData);
rowCount++;
}
log.info("Done writing data.");
log.info(rowCount + " rows written.");
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
throw e;
}
Date end = new Date();
log.info("Converting took {} seconds.", (end.getTime() - start.getTime())/1000);
}