use of com.csvreader.CsvWriter in project gephi by gephi.
the class JTableCSVExporter method writeCSVFile.
/**
* <p>Export a JTable to the specified file.</p>
* @param table Table to export
* @param file File to write
* @param separator Separator to use for separating values of a row in the CSV file. If null ',' will be used.
* @param charset Charset encoding for the file
* @param columnsToExport Indicates the indexes of the columns to export. All columns will be exported if null
* @throws IOException When an error happens while writing the file
*/
public static void writeCSVFile(JTable table, File file, Character separator, Charset charset, Integer[] columnsToExport) throws IOException {
TableModel model = table.getModel();
FileOutputStream out = new FileOutputStream(file);
if (separator == null) {
separator = DEFAULT_SEPARATOR;
}
if (columnsToExport == null) {
columnsToExport = new Integer[model.getColumnCount()];
for (int i = 0; i < columnsToExport.length; i++) {
columnsToExport[i] = i;
}
}
CsvWriter writer = new CsvWriter(out, separator, charset);
//Write column headers:
for (int column = 0; column < columnsToExport.length; column++) {
writer.write(model.getColumnName(columnsToExport[column]), true);
}
writer.endRecord();
//Write rows:
Object value;
String text;
for (int row = 0; row < table.getRowCount(); row++) {
for (int column = 0; column < columnsToExport.length; column++) {
value = model.getValueAt(table.convertRowIndexToModel(row), columnsToExport[column]);
if (value != null) {
text = value.toString();
} else {
text = "";
}
writer.write(text, true);
}
writer.endRecord();
}
writer.close();
}
use of com.csvreader.CsvWriter in project meclipse by flaper87.
the class MeclipsePlugin method saveServers.
private void saveServers() {
// save server preferences here
CsvWriter writer = null;
try {
IPath libPath = getStateLocation();
libPath = libPath.append("servers.cfg");
File file = libPath.toFile();
if (!file.exists()) {
file.createNewFile();
}
writer = new CsvWriter(new FileWriter(file, false), ',');
for (MongoInstance server : mongoInstances.values()) {
writer.write(server.getName());
writer.write(server.getHost());
writer.write(String.valueOf(server.getPort()));
writer.endRecord();
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (writer != null) {
writer.close();
}
}
}
use of com.csvreader.CsvWriter in project gephi by gephi.
the class AttributeTableCSVExporter method writeCSVFile.
/**
* <p>Export a AttributeTable to the specified file.</p>
*
* @param graph Graph containing the table and rows
* @param table Table to export. Cannot be null
* @param out Ouput stream to write. Cannot be null.
* @param separator Separator to use for separating values of a row in the CSV file. If null ',' will be used.
* @param charset Charset encoding for the file. If null, UTF-8 will be used
* @param columnIndexesToExport Indicates the indexes of the columns to export. All columns will be exported if null. For special columns in edges table, use {@code FAKE_COLUMN_EDGE_} values in this class.
* @param rows Elements (table rows: nodes/edges) to include in the exported file. Cannot be null. If null, all nodes/edges will be exported.
* @throws IOException When an error happens while writing the file
*/
public static void writeCSVFile(Graph graph, Table table, OutputStream out, Character separator, Charset charset, Integer[] columnIndexesToExport, Element[] rows) throws IOException {
if (out == null) {
throw new IllegalArgumentException("out cannot be null");
}
if (separator == null) {
separator = DEFAULT_SEPARATOR;
}
if (charset == null) {
charset = Charset.forName("UTF-8");
}
AttributeColumnsController ac = Lookup.getDefault().lookup(AttributeColumnsController.class);
boolean isEdgeTable = ac.isEdgeTable(table);
if (rows == null) {
if (isEdgeTable) {
rows = graph.getEdges().toArray();
} else {
rows = graph.getNodes().toArray();
}
}
TimeFormat timeFormat = graph.getModel().getTimeFormat();
DateTimeZone timeZone = graph.getModel().getTimeZone();
if (columnIndexesToExport == null) {
List<Integer> columnIndexesToExportList = new ArrayList<>();
//Add special columns for edges table:
if (isEdgeTable) {
columnIndexesToExportList.add(FAKE_COLUMN_EDGE_SOURCE);
columnIndexesToExportList.add(FAKE_COLUMN_EDGE_TARGET);
columnIndexesToExportList.add(FAKE_COLUMN_EDGE_TYPE);
}
for (Column column : table) {
columnIndexesToExportList.add(column.getIndex());
}
columnIndexesToExport = columnIndexesToExportList.toArray(new Integer[0]);
}
CsvWriter writer = new CsvWriter(out, separator, charset);
//Write column headers:
for (int column = 0; column < columnIndexesToExport.length; column++) {
int columnIndex = columnIndexesToExport[column];
if (columnIndex == FAKE_COLUMN_EDGE_SOURCE) {
writer.write("Source");
} else if (columnIndex == FAKE_COLUMN_EDGE_TARGET) {
writer.write("Target");
} else if (columnIndex == FAKE_COLUMN_EDGE_TYPE) {
writer.write("Type");
} else {
//Use the title only if it's the same as the id (case insensitive):
String columnId = table.getColumn(columnIndex).getId();
String columnTitle = table.getColumn(columnIndex).getId();
String columnHeader = columnId.equalsIgnoreCase(columnTitle) ? columnTitle : columnId;
writer.write(columnHeader, true);
}
}
writer.endRecord();
//Write rows:
Object value;
String text;
for (int row = 0; row < rows.length; row++) {
for (int i = 0; i < columnIndexesToExport.length; i++) {
int columnIndex = columnIndexesToExport[i];
if (columnIndex == FAKE_COLUMN_EDGE_SOURCE) {
value = ((Edge) rows[row]).getSource().getId();
} else if (columnIndex == FAKE_COLUMN_EDGE_TARGET) {
value = ((Edge) rows[row]).getTarget().getId();
} else if (columnIndex == FAKE_COLUMN_EDGE_TYPE) {
value = ((Edge) rows[row]).isDirected() ? "Directed" : "Undirected";
} else {
value = rows[row].getAttribute(table.getColumn(columnIndex));
}
if (value != null) {
text = AttributeUtils.print(value, timeFormat, timeZone);
} else {
text = "";
}
writer.write(text, true);
}
writer.endRecord();
}
writer.close();
}
use of com.csvreader.CsvWriter in project dhis2-core by dhis2.
the class SpringDataValueSetStore method writeDataValueSetCsv.
@Override
public void writeDataValueSetCsv(DataExportParams params, Date completeDate, Writer writer) {
DataValueSet dataValueSet = new StreamingCsvDataValueSet(new CsvWriter(writer, CSV_DELIM));
String sql = getDataValueSql(params);
writeDataValueSet(sql, params, completeDate, dataValueSet);
IOUtils.closeQuietly(writer);
}
use of com.csvreader.CsvWriter in project dhis2-core by dhis2.
the class GridUtils method toCsv.
/**
* Writes a CSV representation of the given Grid to the given OutputStream.
*/
public static void toCsv(Grid grid, Writer writer) throws IOException {
if (grid == null) {
return;
}
CsvWriter csvWriter = new CsvWriter(writer, CSV_DELIMITER);
Iterator<GridHeader> headers = grid.getHeaders().iterator();
if (!grid.getHeaders().isEmpty()) {
while (headers.hasNext()) {
csvWriter.write(headers.next().getColumn());
}
csvWriter.endRecord();
}
for (List<Object> row : grid.getRows()) {
Iterator<Object> columns = row.iterator();
while (columns.hasNext()) {
Object value = columns.next();
csvWriter.write(value != null ? String.valueOf(value) : StringUtils.EMPTY);
}
csvWriter.endRecord();
}
}
Aggregations