Java Examples for au.com.bytecode.opencsv.CSVWriter
The following java examples will help you to understand the usage of au.com.bytecode.opencsv.CSVWriter. These source code samples are taken from different open source projects.
Example 1
| Project: Bio-PEPA-master File: AddressExample.java View source code |
public static void main(String[] args) throws IOException {
CSVReader reader = new CSVReader(new FileReader(ADDRESS_FILE));
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
System.out.println("Name: [" + nextLine[0] + "]\nAddress: [" + nextLine[1] + "]\nEmail: [" + nextLine[2] + "]");
}
// Try writing it back out as CSV to the console
CSVReader reader2 = new CSVReader(new FileReader(ADDRESS_FILE));
List<String[]> allElements = reader2.readAll();
StringWriter sw = new StringWriter();
CSVWriter writer = new CSVWriter(sw);
writer.writeAll(allElements);
System.out.println("\n\nGenerated CSV File:\n\n");
System.out.println(sw.toString());
}Example 2
| Project: Dead-Reckoning-Android-master File: CsvSample.java View source code |
public void doSample() throws Exception {
// First write
String[] fields = new String[5];
fields[0] = "field1";
fields[1] = "3.0";
fields[2] = "3,147.25";
fields[3] = "$3,147.26";
// failing string
fields[4] = "Joe said, \"This is a test of a \nlong broken string,\" and Sally said, \"I bet it won't work.\" ";
// working string
// fields[4] = "Joe said, \"This is a test of a \nlong broken string,\" and Sally said, \"I bet it won't work.\"";
CSVWriter writer = new CSVWriter(new FileWriter(filePath));
// let's make 3 rows so we can see it cleanly in Excel.
writer.writeNext(fields);
writer.writeNext(fields);
writer.writeNext(fields);
writer.close();
testRawCsvRead(fields[4]);
testMappingStrategyRead(fields[4]);
System.out.println("\nComplete. File written out to " + filePath);
}Example 3
| Project: OpenCSV-3.0-master File: CsvSample.java View source code |
public void doSample() throws Exception {
// First write
String[] fields = new String[5];
fields[0] = "field1";
fields[1] = "3.0";
fields[2] = "3,147.25";
fields[3] = "$3,147.26";
// failing string
fields[4] = "Joe said, \"This is a test of a \nlong broken string,\" and Sally said, \"I bet it won't work.\" ";
// working string
// fields[4] = "Joe said, \"This is a test of a \nlong broken string,\" and Sally said, \"I bet it won't work.\"";
CSVWriter writer = new CSVWriter(new FileWriter(filePath));
// let's make 3 rows so we can see it cleanly in Excel.
writer.writeNext(fields);
writer.writeNext(fields);
writer.writeNext(fields);
writer.close();
testRawCsvRead(fields[4]);
testMappingStrategyRead(fields[4]);
System.out.println("\nComplete. File written out to " + filePath);
}Example 4
| Project: opentrader.github.com-master File: CsvSample.java View source code |
public void doSample() throws Exception {
// First write
String[] fields = new String[5];
fields[0] = "field1";
fields[1] = "3.0";
fields[2] = "3,147.25";
fields[3] = "$3,147.26";
// failing string
fields[4] = "Joe said, \"This is a test of a \nlong broken string,\" and Sally said, \"I bet it won't work.\" ";
// working string
// fields[4] = "Joe said, \"This is a test of a \nlong broken string,\" and Sally said, \"I bet it won't work.\"";
CSVWriter writer = new CSVWriter(new FileWriter(filePath));
// let's make 3 rows so we can see it cleanly in Excel.
writer.writeNext(fields);
writer.writeNext(fields);
writer.writeNext(fields);
writer.close();
testRawCsvRead(fields[4]);
testMappingStrategyRead(fields[4]);
System.out.println("\nComplete. File written out to " + filePath);
}Example 5
| Project: sad-analyzer-master File: CsvSample.java View source code |
public void doSample() throws Exception {
// First write
String[] fields = new String[5];
fields[0] = "field1";
fields[1] = "3.0";
fields[2] = "3,147.25";
fields[3] = "$3,147.26";
// failing string
fields[4] = "Joe said, \"This is a test of a \nlong broken string,\" and Sally said, \"I bet it won't work.\" ";
// working string
// fields[4] = "Joe said, \"This is a test of a \nlong broken string,\" and Sally said, \"I bet it won't work.\"";
CSVWriter writer = new CSVWriter(new FileWriter(filePath));
// let's make 3 rows so we can see it cleanly in Excel.
writer.writeNext(fields);
writer.writeNext(fields);
writer.writeNext(fields);
writer.close();
testRawCsvRead(fields[4]);
testMappingStrategyRead(fields[4]);
System.out.println("\nComplete. File written out to " + filePath);
}Example 6
| Project: testCmpe131-master File: CsvSample.java View source code |
public void doSample() throws Exception {
// First write
String[] fields = new String[5];
fields[0] = "field1";
fields[1] = "3.0";
fields[2] = "3,147.25";
fields[3] = "$3,147.26";
// failing string
fields[4] = "Joe said, \"This is a test of a \nlong broken string,\" and Sally said, \"I bet it won't work.\" ";
// working string
// fields[4] = "Joe said, \"This is a test of a \nlong broken string,\" and Sally said, \"I bet it won't work.\"";
CSVWriter writer = new CSVWriter(new FileWriter(filePath));
// let's make 3 rows so we can see it cleanly in Excel.
writer.writeNext(fields);
writer.writeNext(fields);
writer.writeNext(fields);
writer.close();
testRawCsvRead(fields[4]);
testMappingStrategyRead(fields[4]);
System.out.println("\nComplete. File written out to " + filePath);
}Example 7
| Project: egonet-master File: ConsensusDataWriter.java View source code |
@SuppressWarnings("unchecked")
public String writeToFile(File file) throws IOException {
Set<WholeNetworkAlter> allAlters = Sets.newHashSet(net.getWholeNetworkAlters().values());
Integer numTies = 0;
Integer numMixedTies = 0;
Integer numYesOnlyTies = 0;
Integer numNoOnlyTies = 0;
Integer numUntied = 0;
Integer numTiedWithoutVotes = 0;
Map<WholeNetworkAlter, Set<Integer>> reportersByAlter = Maps.newHashMap();
Set<Integer> allReporters = Sets.newHashSet();
for (WholeNetworkAlter alter1 : allAlters) {
Set<Integer> alter1reporters = Sets.newHashSet();
for (WholeNetworkAlter alter2 : allAlters) {
if (!alter1.equals(alter2)) {
WholeNetworkTie tie = net.getTie(alter1, alter2);
if (tie == null) {
numUntied++;
} else {
numTies++;
Set<Integer> tieReporters = Sets.union(tie.tiedNo(), tie.tiedYes());
alter1reporters.addAll(tieReporters);
if (tieReporters.isEmpty()) {
numTiedWithoutVotes++;
} else if (tie.tiedNo().isEmpty()) {
numYesOnlyTies++;
} else if (tie.tiedYes().isEmpty()) {
numNoOnlyTies++;
} else {
numMixedTies++;
}
}
}
}
reportersByAlter.put(alter1, alter1reporters);
allReporters.addAll(alter1reporters);
}
logger.info("NumTies: " + numTies + ", NumMixed: " + numMixedTies + ", NumYesOnly: " + numYesOnlyTies + ", NumNoOnly: " + numNoOnlyTies + ", NumUntied: " + numUntied + ", NumTiedWithoutVotes: " + numTiedWithoutVotes);
Map<Integer, Set<WholeNetworkAlter>> altersByReporter = Maps.newHashMap();
for (Integer reporter : allReporters) {
Set<WholeNetworkAlter> reporterAlters = Sets.newHashSet();
for (WholeNetworkAlter alter : allAlters) {
if (reportersByAlter.get(alter).contains(reporter)) {
reporterAlters.add(alter);
}
}
altersByReporter.put(reporter, reporterAlters);
}
// Construct two mode network of alters and reporters and look for cliques,
// which are subnetworks in which all reporters can report on all alters.
// Add connections for 100% density within modes so that I can use a
// one mode clique finding algorithm.
Map<Object, Set> alterAndReporterTwoMode = Maps.newHashMap();
for (Integer reporter : allReporters) {
Set<Object> reporterConnections = Sets.newHashSet();
reporterConnections.addAll(altersByReporter.get(reporter));
alterAndReporterTwoMode.put(reporter, reporterConnections);
}
for (WholeNetworkAlter alter : allAlters) {
Set<Object> alterConnections = Sets.newHashSet();
alterConnections.addAll(reportersByAlter.get(alter));
alterAndReporterTwoMode.put(alter, alterConnections);
}
Set<Object> kplex = new KPlexesTwoMode().findLargeKPlex(alterAndReporterTwoMode, allReporters, allowedMissingEdgesPerNode);
Set<Integer> kplexReporters = Sets.intersection(allReporters, kplex);
Set<WholeNetworkAlter> kplexAlters = Sets.intersection(allAlters, kplex);
List<WholeNetworkAlter> alterList = Lists.newArrayList(kplexAlters);
List<Integer> reporterList = Lists.newArrayList(kplexReporters);
FileWriter fw = new FileWriter(file);
CSVWriter csv = new CSVWriter(fw);
List<String> header = Lists.newArrayList("");
for (int i = 0; i < alterList.size(); i++) {
WholeNetworkAlter alter1 = alterList.get(i);
for (int j = i + 1; j < alterList.size(); j++) {
WholeNetworkAlter alter2 = alterList.get(j);
if (!alter1.equals(alter2)) {
header.add(alter1 + " - " + alter2);
}
}
}
csv.writeNext(header.toArray(new String[] {}));
Integer dataYes = 0, dataNo = 0, guessYes = 0, guessNo = 0;
Integer guessWithKnownAlters = 0;
Integer guessWithUnknownAlters = 0;
Random rand = new Random();
for (Integer reporter : reporterList) {
List<String> line = Lists.newArrayList(reporter + "");
for (int i = 0; i < alterList.size(); i++) {
WholeNetworkAlter alter1 = alterList.get(i);
for (int j = i + 1; j < alterList.size(); j++) {
WholeNetworkAlter alter2 = alterList.get(j);
WholeNetworkTie tie = net.getTie(alter1, alter2);
if (alterAndReporterTwoMode.get(reporter).contains(alter1) && alterAndReporterTwoMode.get(reporter).contains(alter2)) {
if (alter1.getId().equals(reporter) || alter2.getId().equals(reporter)) // Tie between ego and alter
{
line.add("1");
dataYes++;
} else if (tie != null && tie.tiedYes().contains(reporter)) {
line.add("1");
dataYes++;
} else if (tie != null && tie.tiedNo().contains(reporter)) {
line.add("0");
dataNo++;
} else {
guessWithKnownAlters++;
System.out.println(reporter + " knows " + alter1 + " and " + alter2 + " but didn't say whether they are linked.");
}
} else {
if (rand.nextInt(2) > 0) {
line.add("1");
guessYes++;
} else {
line.add("0");
guessNo++;
}
guessWithUnknownAlters++;
}
}
}
csv.writeNext(line.toArray(new String[] {}));
}
Integer reportedAnswers = dataYes + dataNo;
Integer imputedAnswers = guessYes + guessNo;
Double portionImputed = imputedAnswers * 1.0 / (reportedAnswers + imputedAnswers);
String msg = "Allowed max missing per node: " + allowedMissingEdgesPerNode + "\nActual max missing per node: " + new KPlexesTwoMode().maxMissingEdgesPerNodeInSubgroup(alterAndReporterTwoMode, allReporters, kplex) + "\nReporters: " + reporterList.size() + "\nAlters: " + alterList.size() + "\nReported answers: " + reportedAnswers + "\nImputed answers: " + imputedAnswers + "\nPercent imputed: " + ((int) (portionImputed * 100)) + "%" + "\nReporter didn't know one or both alters: " + guessWithUnknownAlters + "\nReporter knew alters, but I couldn't find answer: " + guessWithKnownAlters;
System.out.println(msg);
csv.flush();
fw.close();
return msg;
}Example 8
| Project: foxtrot-master File: FlatToCsvConverter.java View source code |
public static void convert(final FlatRepresentation representation, Writer writer) throws IOException {
CSVWriter data = new CSVWriter(writer);
List<FieldHeader> headers = representation.getHeaders();
String headerNames[] = new String[headers.size()];
int i = 0;
for (FieldHeader fieldHeader : headers) {
headerNames[i++] = fieldHeader.getName();
}
data.writeNext(headerNames);
List<Map<String, Object>> rows = representation.getRows();
for (Map<String, Object> row : rows) {
String rowData[] = new String[row.size()];
i = 0;
for (FieldHeader fieldHeader : headers) {
rowData[i++] = row.get(fieldHeader.getName()).toString().replaceAll("\"", "").replaceAll("null", "");
}
data.writeNext(rowData);
}
data.close();
}Example 9
| Project: groundhog-master File: CSVFormater.java View source code |
@Override
public String format(HashMap<String, HashMap<String, MutableInt>> object) {
StringWriter result = new StringWriter();
try {
CSVWriter writer = new CSVWriter(result, ';');
writer.writeNext(new String[] { "Metric", "Entry", "Value" });
for (String metric : object.keySet()) {
HashMap<String, MutableInt> counter = object.get(metric);
for (Entry<String, MutableInt> entry : counter.entrySet()) {
writer.writeNext(new String[] { metric, entry.getKey(), entry.getValue().toString() });
}
}
writer.flush();
writer.close();
return result.toString();
} catch (IOException e) {
e.printStackTrace();
throw new ParserException();
}
}Example 10
| Project: mssqldiff-master File: SchemaCsvWriter.java View source code |
public void write(List<SchemaCsv> data) {
try {
OutputStream output = new FileOutputStream(path);
Writer iwriter = new OutputStreamWriter(output, "UTF-8");
CSVWriter writer = new CSVWriter(iwriter, ',', '"');
try {
writer.writeNext(SchemaCsv.COLUMNS);
for (SchemaCsv csv : data) {
String[] values = new String[SchemaCsv.COLUMNS.length];
values[0] = csv.getObjectType();
values[1] = csv.getTableName();
values[2] = csv.getColumnName();
values[3] = csv.getColumnType();
values[4] = Integer.toString(csv.getLength());
values[5] = Boolean.toString(csv.isPk());
values[6] = Boolean.toString(csv.isIdentity());
values[7] = Boolean.toString(csv.isNullable());
values[8] = csv.getDefaultValue();
values[9] = csv.getTableDescription();
values[10] = csv.getColumnDescription();
values[11] = csv.getUserName();
values[12] = Boolean.toString(csv.isCanSelect());
values[13] = Boolean.toString(csv.isCanInsert());
values[14] = Boolean.toString(csv.isCanUpdate());
values[15] = Boolean.toString(csv.isCanDelete());
writer.writeNext(values);
}
} finally {
writer.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}Example 11
| Project: TadpoleForDBTools-master File: CSVFileUtils.java View source code |
/**
* csv data
* @param seprator
*
* @param
*/
public static String makeData(List<String[]> listContent, char seprator) throws Exception {
String strReust = "";
StringWriter sw = new StringWriter();
CSVWriter writer = null;
try {
writer = new CSVWriter(sw, seprator);
writer.writeAll(listContent);
strReust = sw.getBuffer().toString();
} finally {
if (writer != null)
try {
writer.close();
} catch (Exception e) {
}
sw.close();
}
return strReust;
}Example 12
| Project: TrackIt-master File: TestRecord.java View source code |
public void updateLog(GameState state, boolean shortCircuited) throws IOException {
// If we call this before setting up the state, return gracefully
if (state.getIO() == null) {
return;
}
@SuppressWarnings("resource") CSVWriter writer = new CSVWriter(state.getIO(), ',');
while (state.hasMoreToWrite()) {
writer.writeNext(state.nextDataLine());
}
// make a mark in the .csv file
if (shortCircuited) {
writer.writeNext(new String[] { "End of entire file." });
}
writer.flush();
//writer.close();
}Example 13
| Project: android-mileage-master File: CSVView.java View source code |
public void run() {
try {
CSVWriter csv = new CSVWriter(new FileWriter(Environment.getExternalStorageDirectory() + "/" + getFilename()));
HashMap<String, String> fillupsProjection = FillUpsProvider.getFillUpsProjection();
Set<String> tmp_keySet = fillupsProjection.keySet();
ArrayList<String> keySet = new ArrayList<String>(tmp_keySet);
keySet.remove(FillUps._ID);
String[] proj = keySet.toArray(new String[keySet.size()]);
SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/" + Mileage.PACKAGE + "/databases/" + FillUpsProvider.DATABASE_NAME, null, SQLiteDatabase.OPEN_READONLY);
Cursor c = db.query(FillUpsProvider.FILLUPS_TABLE_NAME, proj, null, null, null, null, FillUps._ID + " ASC");
c.moveToFirst();
// transform the column keySet into the plain English form
int i = 0;
for (String key : keySet) {
String english = FillUps.PLAINTEXT.get(key);
if (english != null) {
keySet.set(i, english);
}
i++;
}
csv.writeNext(keySet.toArray(new String[keySet.size()]));
while (c.isAfterLast() == false) {
String[] data = new String[keySet.size()];
for (i = 0; i < c.getColumnCount(); i++) {
data[i] = c.getString(i);
}
csv.writeNext(data);
c.moveToNext();
}
c.close();
csv.close();
m_handler.post(new Runnable() {
public void run() {
Bundle data = new Bundle();
data.putString(MESSAGE, getString(R.string.export_finished_msg) + "\n" + getFilename());
data.putString(TITLE, getString(R.string.success));
Message msg = new Message();
msg.setData(data);
m_handler.handleMessage(msg);
}
});
} catch (final IOException e) {
e.printStackTrace();
m_handler.post(new Runnable() {
public void run() {
Bundle data = new Bundle();
data.putString(MESSAGE, e.getMessage());
data.putString(TITLE, getString(R.string.error));
Message msg = new Message();
msg.setData(data);
m_handler.handleMessage(msg);
}
});
return;
}
}Example 14
| Project: dtf-master File: CSVRecorder.java View source code |
public void start() throws RecorderException {
String file;
try {
file = Action.getStorageFactory().getPath(_uri);
_writer = new CSVWriter(new FileWriter(file, _append));
} catch (StorageException e) {
throw new RecorderException("Error opening csv file.");
} catch (IOException e) {
throw new RecorderException("Error opening csv file.");
}
}Example 15
| Project: fancy-chart-master File: CsvDao.java View source code |
public static void exportCsv(SortedMap<Number, Number> data, String filePath) {
try {
writer = new CSVWriter(new FileWriter(filePath), ',', '\0');
List<String[]> entries = new ArrayList<>(data.size());
for (Number x : data.keySet()) {
String xString = String.valueOf(x);
String yString = String.valueOf(data.get(x));
entries.add(new String[] { xString, yString });
}
writer.writeAll(entries);
writer.flush();
writer.close();
} catch (IOException e) {
System.err.println(e.getMessage());
}
}Example 16
| Project: fork-master File: ResultsStorage.java View source code |
public void storeResults(Map<TestCaseEvent, Double> results) throws ResultStorageException {
FileWriter writer = null;
CSVWriter csvWriter = null;
try {
writer = new FileWriter(new File(output, TIMINGS_FILE));
csvWriter = new CSVWriter(writer);
write(results, csvWriter);
} catch (IOException e) {
throw new ResultStorageException("Failed to write results", e);
} finally {
closeQuietly(csvWriter);
closeQuietly(writer);
}
}Example 17
| Project: quickml-master File: InstancesToCsvWriter.java View source code |
public static void writeInstances(List<? extends Instance<AttributesMap, Serializable>> instances, String filePath) {
List<String[]> allRows = Lists.newLinkedList();
String[] header = getHeader(instances);
allRows.add(header);
for (Instance<AttributesMap, Serializable> instance : instances) {
allRows.add(instanceToRow(instance, header));
}
File file = new File(filePath);
Writer writer = null;
try {
writer = new BufferedWriter(new FileWriter(file));
CSVWriter csvWriter = new CSVWriter(writer);
csvWriter.writeAll(allRows);
writer.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}Example 18
| Project: saos-master File: ChartCsvExporter.java View source code |
//------------------------ LOGIC --------------------------
/**
* Converts the given chart to csv and writes the csv to the given writer
* @throws IOException in case of I/O Error during writing to the writer
*/
public void exportChartToCsv(Chart<Object, Number> chart, ChartCode chartCode, AnalysisForm analysisForm, Locale locale, Writer writer) throws IOException {
CSVWriter csvWriter = createCsvWriter(writer);
try {
csvWriter.writeNext(chartCsvGenerator.generateHeader(chartCode, analysisForm, locale));
int rowCount = chart.getSeriesList().get(0).getPoints().size();
for (int i = 0; i < rowCount; ++i) {
csvWriter.writeNext(chartCsvGenerator.generateRow(chart, i));
}
} finally {
csvWriter.close();
}
}Example 19
| Project: solrmeter-master File: FullQueryStatisticController.java View source code |
public void writeCSV(File file) throws IOException {
FileWriter writer = new FileWriter(file);
CSVWriter csvWriter = new CSVWriter(writer);
for (QueryLogValue query : queryLogStatistic.getLastQueries()) {
String[] cols = new String[] { String.valueOf(query.isError()), query.getQueryString(), query.getFacetQueryString(), query.getFilterQueryString(), query.getQTime().toString(), query.getResults().toString() };
csvWriter.writeNext(cols);
}
csvWriter.close();
}Example 20
| Project: wordnet-extras-master File: Wn30CoreToWn31.java View source code |
public static void main(String[] args) throws IOException {
Preconditions.checkArgument(args.length == 2, "Usage: wn30core-to-wn31 path/to/wn30-core-synsets.tab dest/dir");
ArrayList<String> wn31senses = new ArrayList<>();
Model model = ModelFactory.createDefaultModel();
model.setNsPrefix("wn31", "http://wordnet-rdf.princeton.edu/wn31/");
model.setNsPrefix("wordnet-ontology", "http://wordnet-rdf.princeton.edu/ontology#");
model.setNsPrefix("olo", "http://purl.org/ontology/olo/core#");
model.setNsPrefix("xsd", XSD.getURI());
Resource synsetRes = model.createResource(model.getNsPrefixURI("wordnet-ontology") + "Synset");
Property indexProp = model.createProperty(model.getNsPrefixURI("olo"), "index");
File wn30coreFile = new File(args[0]);
log.info("Loading WordNet 3.0 core file '{}'...", wn30coreFile);
try (CSVReader reader = new CSVReader(new FileReader(wn30coreFile), '\t')) {
int index = 1;
while (true) {
String[] row = reader.readNext();
if (row == null) {
break;
}
String wn30sense = row[0];
char posLetter = wn30sense.charAt(9);
final Integer posNumeric = Preconditions.checkNotNull(POS_NUMERIC.get(posLetter), "Invalid part-of-speech letter code '%s' for sense '%s'", posLetter, wn30sense);
String wn31sense = posNumeric + wn30sense;
wn31senses.add(wn31sense);
final Resource wn31res = model.createResource(model.getNsPrefixURI("wn31") + wn31sense);
model.add(wn31res, RDF.type, synsetRes);
// hacky usage, but pragmatic :P
model.addLiteral(wn31res, indexProp, index);
index++;
}
}
log.info("Converted to {} RDF statements", Iterators.size(model.listStatements()));
File wn31TurtleFile = new File(args[1], "wn31-core-synsets.ttl");
log.info("Saving TURTLE to '{}'...", wn31TurtleFile);
RDFDataMgr.write(new FileOutputStream(wn31TurtleFile), model, RDFFormat.TURTLE);
File wn31TsvFile = new File(args[1], "wn31-core-synsets.tab");
log.info("Saving TSV to '{}'...", wn31TsvFile);
try (CSVWriter writer = new CSVWriter(new FileWriter(wn31TsvFile), '\t', CSVWriter.NO_QUOTE_CHARACTER)) {
for (String wn31sense : wn31senses) {
writer.writeNext(new String[] { wn31sense });
}
}
}Example 21
| Project: xchain-master File: CsvWriterChain.java View source code |
public boolean execute(JXPathContext context) throws Exception {
// Do some initial error checking.
if (!hasOutputStream() && !hasCSVWriter())
throw new Exception("Must set either an output stream or a csv writer.");
if (AbstractCsvChain.csvWriterThreadLocal.get() != null)
throw new Exception("Nesting of CsvWriterChain instances is not permitted.");
boolean result = false;
CSVWriter writer = null;
// If we have an output stream, use it to create a new CSVWriter.
if (hasOutputStream()) {
OutputStream output = this.getOutputStream(context);
Writer streamWriter = new OutputStreamWriter(output);
// writer = new CSVWriter(streamWriter, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER);
writer = new CSVWriter(streamWriter);
}
// If we have a CSVWriter... then lets use it.
if (hasCSVWriter())
writer = this.getCSVWriter(context);
try {
// We put the writer in a thread local so that the row and cell chains can make use of it.
AbstractCsvChain.csvWriterThreadLocal.set(writer);
result = super.execute(context);
} finally {
AbstractCsvChain.csvWriterThreadLocal.set(null);
}
writer.close();
return result;
}Example 22
| Project: XCoLab-master File: CsvExportHelper.java View source code |
private String getCSVData() throws IOException {
StringWriter writer = new StringWriter();
CSVWriter csvWriter = new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.DEFAULT_QUOTE_CHARACTER);
csvWriter.writeAll(records);
csvWriter.close();
String separatorIndicationForExcel = "sep=" + CSVWriter.DEFAULT_SEPARATOR + CSVWriter.DEFAULT_LINE_END;
return separatorIndicationForExcel + writer.toString();
}Example 23
| Project: bricklink-master File: Info4LEGOarticles.java View source code |
public static void main(String[] args) throws Exception {
File file = new File(args[0]);
List<String[]> input;
try (FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
CSVReader csvr = new CSVReader(isr, ';', CSVWriter.DEFAULT_QUOTE_CHARACTER)) {
input = csvr.readAll();
}
Configuration configuration = new Configuration();
BrickLinkClient client = new BrickLinkClient(configuration);
List<String[]> output = new ArrayList<String[]>();
try {
for (String[] line : input) {
List<String> row = new ArrayList<String>();
for (String tmp : line) {
row.add(tmp);
}
String legoId = line[0];
int identifier;
try {
identifier = Integer.parseInt(legoId);
} catch (NumberFormatException e) {
throw e;
}
ItemNumberResponse response = client.execute(new ItemNumberRequest(identifier));
if (response.getItemMapping().size() != 1) {
System.out.println(response.getItemMapping().toString());
output.add(row.toArray(new String[row.size()]));
continue;
}
ItemMapping mapping = response.getItemMapping().get(0);
System.out.println(mapping.toString());
String itemID = mapping.getItem().getIdentifier();
row.add(itemID);
int colorID = mapping.getColor().getIdentifier();
row.add(Integer.toString(colorID));
ItemType type = mapping.getType() == null ? mapping.getItem().getType() : mapping.getType();
PriceGuide soldGuide = client.execute(new PriceGuideRequest(type, itemID, colorID, GuideType.SOLD, Condition.N)).getPriceGuide();
PriceGuide stockGuide = client.execute(new PriceGuideRequest(type, itemID, colorID, GuideType.STOCK, Condition.N)).getPriceGuide();
BigDecimal quantityAveragePrice = soldGuide.getQuantityAveragePrice();
row.add(quantityAveragePrice.toString());
row.add(Integer.toString(stockGuide.getQuantity()));
row.add(Integer.toString(soldGuide.getQuantity()));
output.add(row.toArray(new String[row.size()]));
}
} finally {
client.close();
}
FileOutputStream fis = null;
OutputStreamWriter osw = null;
CSVWriter csvw = null;
try {
fis = new FileOutputStream(new File("lugbulk.csv"));
osw = new OutputStreamWriter(fis, "UTF-8");
csvw = new CSVWriter(osw, ';', CSVWriter.DEFAULT_QUOTE_CHARACTER, CSVWriter.DEFAULT_ESCAPE_CHARACTER, "\n");
csvw.writeAll(output);
} finally {
IOUtils.closeQuietly(osw);
IOUtils.closeQuietly(fis);
if (null != csvw) {
csvw.close();
}
}
}Example 24
| Project: brigen-base-master File: CsvDelegaterOpenCsv.java View source code |
@Override
public <T> void format(List<T> datas, IFormatStrategy<T> strategy, Writer out) throws CsvException {
try (CSVWriter writer = new CSVWriter(out, strategy.getSeparator(), strategy.getQuote(), strategy.getEscape(), strategy.getLineEnd())) {
IDataConverter<T> converter = strategy.getConverter();
{
String[] header = converter.header();
if (header != null) {
writer.writeNext(header);
}
}
for (T data : datas) {
writer.writeNext(converter.convertToLine(data));
}
} catch (IOException e) {
throw new CsvException(e);
}
}Example 25
| Project: haikudepotserver-master File: PkgIconImportArchiveJobRunner.java View source code |
@Override
public void run(JobService jobService, PkgIconImportArchiveJobSpecification specification) throws IOException, JobRunnerException {
Preconditions.checkArgument(null != jobService);
Preconditions.checkArgument(null != specification);
Preconditions.checkArgument(null != specification.getInputDataGuid(), "missing input data guid on specification");
// this will register the outbound data against the job.
JobDataWithByteSink jobDataWithByteSink = jobService.storeGeneratedData(specification.getGuid(), "download", MediaType.CSV_UTF_8.toString());
Optional<JobDataWithByteSource> jobDataWithByteSourceOptional = jobService.tryObtainData(specification.getInputDataGuid());
if (!jobDataWithByteSourceOptional.isPresent()) {
throw new IllegalStateException("the job data was not able to be found for guid; " + specification.getInputDataGuid());
}
// start a cayenne long-running txn
Transaction transaction = serverRuntime.getDataDomain().createTransaction();
Transaction.bindThreadTransaction(transaction);
try (OutputStream outputStream = jobDataWithByteSink.getByteSink().openBufferedStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
CSVWriter writer = new CSVWriter(outputStreamWriter, ',')) {
String[] headings = new String[] { "path", "action", "message" };
writer.writeNext(headings);
try (InputStream inputStream = jobDataWithByteSourceOptional.get().getByteSource().openStream();
GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream);
TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(gzipInputStream)) {
clearPackagesIconsAppearingInArchive(tarArchiveInputStream, writer);
}
try (InputStream inputStream = jobDataWithByteSourceOptional.get().getByteSource().openStream();
GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream);
TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(gzipInputStream)) {
processEntriesFromArchive(tarArchiveInputStream, writer);
}
transaction.commit();
} catch (SQLExceptionCayenneException | e) {
throw new JobRunnerException("unable to complete job; ", e);
} finally {
Transaction.bindThreadTransaction(null);
if (Transaction.STATUS_MARKED_ROLLEDBACK == transaction.getStatus()) {
try {
transaction.rollback();
} catch (Exception e) {
}
}
}
}Example 26
| Project: hale-master File: CSVInstanceWriter.java View source code |
/**
* @see eu.esdihumboldt.hale.common.core.io.impl.AbstractIOProvider#execute(eu.esdihumboldt.hale.common.core.io.ProgressIndicator,
* eu.esdihumboldt.hale.common.core.io.report.IOReporter)
*/
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
boolean solveNestedProperties = getParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES).as(Boolean.class, false);
// XXX what does "solve nested properties" mean?
// get separation, quote and escape sign
sep = CSVUtil.getSep(this);
quote = CSVUtil.getQuote(this);
esc = CSVUtil.getEscape(this);
List<String> headerRow = new ArrayList<String>();
// get the parameter to get the type definition
String exportType = getParameter(InstanceTableIOConstants.EXPORT_TYPE).as(String.class);
QName selectedTypeName = null;
if (exportType != null && !exportType.equals("") && !exportType.equals(" ")) {
selectedTypeName = QName.valueOf(exportType);
}
// get all instances of the selected Type
InstanceCollection instances = getInstanceCollection(selectedTypeName);
Iterator<Instance> instanceIterator = instances.iterator();
Instance instance = null;
try {
instance = instanceIterator.next();
} catch (NoSuchElementException e) {
reporter.error(new IOMessageImpl("There are no instances for the selected type.", e));
return reporter;
}
// get definition of current instance (only this properties with this
// definition type will be written to csv file)
TypeDefinition definition = instance.getDefinition();
// first csv file doesn't have a header row, so it' necessary to write
// it to a temp directory
File tempDir = Files.createTempDir();
File tempFile = new File(tempDir, "tempInstances.csv");
// write instances to csv file (without header)
CSVWriter writer = new CSVWriter(new OutputStreamWriter(new FileOutputStream(tempFile)), sep, quote, esc);
writeLine(solveNestedProperties, headerRow, instance, writer);
while (instanceIterator.hasNext()) {
Instance nextInst = instanceIterator.next();
if (nextInst.getDefinition().equals(definition)) {
writeLine(solveNestedProperties, headerRow, nextInst, writer);
}
}
writer.close();
// header is only finished if all properties have been processed
// insert header to temp file and write it to output
insertHeader(tempFile, getTarget().getOutput(), headerRow);
FileUtils.deleteDirectory(tempDir);
reporter.setSuccess(true);
return reporter;
}Example 27
| Project: mobilitytestbed-master File: ResultParser.java View source code |
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
CSVWriter csvWriter = new CSVWriter(new FileWriter(new File("merge_result.csv")));
String[] header = new String[] { "Exp. number", "Request number", "Driver number", "Uni. dist driver", "Uni. dist, passenger", "Average passenger travel time (on-board) is", "Max passenger travel time (on-board) is", "Median passenger travel time (on-board) is", "Average passenger ride time (on-board) is", "Max passenger ride time (on-board) is", "Median passenger ride time (on-board) is", "Average passenger wait time is", "Max passenger wait time is", "Median passenger wait time is", "Total vehicle distance driven (in kilometers)", "Total values of CO2 [gram]", "Total values of CO [gram]", "Total values of NOx [gram]", "Total values of PM10 [gram]", "Total values of SOx [gram]", "Total values of [Liter] fuel" };
csvWriter.writeNext(header);
File resultsFile = new File("results");
List<File> files = Lists.newArrayList(resultsFile.listFiles());
Collections.sort(files, new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
return Integer.valueOf(o1.getName()) - Integer.valueOf(o2.getName());
}
});
for (File result : files) {
String[] line = new String[header.length];
line[0] = result.getName();
File[] resultDataFiles = result.listFiles(new FilenameFilter() {
@Override
public boolean accept(File arg0, String arg1) {
return arg1.indexOf("result_testbed_san_francisco") != -1;
}
});
if (resultDataFiles == null || resultDataFiles.length == 0) {
continue;
}
File resultDataFile = resultDataFiles[0];
String[] simConfig = resultDataFile.getName().split("_");
line[1] = simConfig[5].replace("r", "").trim();
line[2] = simConfig[6].replace("d", "").trim();
line[3] = simConfig[7].replace("DU", "").trim();
line[4] = simConfig[8].replace("PU", "").replace(".txt", "").trim();
List<String> factors = FileUtils.readLines(resultDataFile).subList(1, 17);
int ind = 5;
for (String factor : factors) {
line[ind++] = factor.substring(factor.indexOf(":") + 1, factor.length()).trim();
}
csvWriter.writeNext(line);
}
csvWriter.close();
}Example 28
| Project: movsim-master File: OutputWriter.java View source code |
public void write(List<ConsumptionDataRecord> records) {
CSVWriter writer = null;
try {
writer = new CSVWriter(new FileWriter(output), SEPARATOR_CHARACTER, QUOTE_CHARACTER);
if (!records.isEmpty()) {
writer.writeNext(records.get(0).csvHeader(String.valueOf(SEPARATOR_CHARACTER)));
for (ConsumptionDataRecord record : records) {
// feed in your array (or convert your data to an array)
writer.writeNext(record.toCsv(String.valueOf(SEPARATOR_CHARACTER)));
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
}
}
}
}Example 29
| Project: OG-Platform-master File: ViewportResultsJsonCsvWriter.java View source code |
public String getCsv(ViewportResults viewportResults) {
GridColumnGroups columnGroups = viewportResults.getColumns();
String[] header1 = new String[columnGroups.getGroups().size()];
String[] header2 = new String[columnGroups.getColumnCount()];
int index = 0;
for (GridColumnGroup gridColumnGroup : columnGroups.getGroups()) {
header1[index++] = gridColumnGroup.getName();
}
List<GridColumn> columns = columnGroups.getColumns();
index = 0;
for (GridColumn gridColumn : columns) {
header2[index++] = gridColumn.getHeader();
}
StringWriter stringWriter = new StringWriter();
@SuppressWarnings("resource") CSVWriter csvWriter = new CSVWriter(stringWriter);
csvWriter.writeNext(header1);
csvWriter.writeNext(header2);
List<ResultsCell> viewportCells = viewportResults.getResults();
Iterable<List<ResultsCell>> results = Iterables.partition(viewportCells, columnGroups.getColumnCount());
for (List<ResultsCell> row : results) {
String[] rowArray = new String[row.size()];
int col = 0;
for (ResultsCell cell : row) {
Object cellValue = cell.getValue();
if (cellValue instanceof RowTarget) {
rowArray[col++] = ((RowTarget) cellValue).getName();
continue;
}
ValueSpecification cellValueSpec = cell.getValueSpecification();
Object formattedValue = _formatter.format(cellValue, cellValueSpec, cell.getFormat(), cell.getInlineKey());
if (formattedValue instanceof String) {
rowArray[col++] = (String) formattedValue;
} else {
rowArray[col++] = formattedValue.toString();
}
}
csvWriter.writeNext(rowArray);
}
return stringWriter.toString();
}Example 30
| Project: PhenotypeArchive-master File: ControllerUtils.java View source code |
public static void writeAsCSV(List<String[]> toWrite, String fileName, HttpServletResponse response) throws IOException {
response.setContentType("text/csv;charset=utf-8");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
OutputStream resOs = response.getOutputStream();
OutputStream buffOs = new BufferedOutputStream(resOs);
CSVWriter writer = new CSVWriter(new OutputStreamWriter(buffOs));
writer.writeAll(toWrite);
writer.close();
}Example 31
| Project: tika-master File: CSVMessageBodyWriter.java View source code |
@Override
@SuppressWarnings("resource")
public void writeTo(Metadata metadata, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
CSVWriter writer = new CSVWriter(new OutputStreamWriter(entityStream, UTF_8));
for (String name : metadata.names()) {
String[] values = metadata.getValues(name);
ArrayList<String> list = new ArrayList<String>(values.length + 1);
list.add(name);
list.addAll(Arrays.asList(values));
writer.writeNext(list.toArray(values));
}
// Don't close, just flush the stream
writer.flush();
}Example 32
| Project: taukari-master File: CsvManager.java View source code |
/**
* Copies a CSV file. It can filter columns and copy only a subset of the
* lines of the input file.
*
* @param sourcePath
* the full path of the source file
* @param destPath
* the full path of the destination file
* @param columns
* the columns to copy. All columns are copied if null.
* @param sampling
* a ratio of samples to copy. 0 means no sample, 1 or more or
* null means all samples.
*/
public void copyCSV(String sourcePath, String destPath, Set<String> columns, Double sampling) {
Random rand = new Random();
CSVReader reader = null;
CSVWriter writer = null;
try {
reader = new CSVReader(new FileReader(sourcePath));
writer = new CSVWriter(new FileWriter(destPath), DEFAULT_SEPARATOR, NO_QUOTE_CHARACTER);
// Handling header
List<Integer> selectedColumns = new ArrayList<Integer>();
String[] inputLine = reader.readNext();
for (int i = 0; i < inputLine.length; i++) {
if (columns == null || columns.contains(inputLine[i])) {
selectedColumns.add(i);
}
}
// Write
boolean isHeaderLine = true;
if (selectedColumns.size() > 0) {
do {
if (isHeaderLine || sampling == null || rand.nextDouble() < sampling) {
String[] outputLine = new String[selectedColumns.size()];
boolean okLine = true;
for (int i = 0; i < selectedColumns.size(); i++) {
if (inputLine.length > selectedColumns.get(i)) {
outputLine[i] = inputLine[selectedColumns.get(i)];
} else {
okLine = false;
break;
}
}
if (okLine) {
writer.writeNext(outputLine);
}
}
isHeaderLine = false;
} while ((inputLine = reader.readNext()) != null);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(reader);
IOUtils.closeQuietly(writer);
}
}Example 33
| Project: AIDR-master File: CVSRemoteFileFormatter.java View source code |
public CSVWriter instanceToOutput(String fileName) throws Exception { File file = new File(fileName); //file.getAbsolutePath(); CSVWriter writer = new CSVWriter(new FileWriter(fileName, true)); // public MicromapperOuput(String tweetID, String tweet, String author, String lat, String lng, String url, String created, String answer){ String[] header = { "tweetID", "tweet", "author", "lat", "lng", "url", "created", "answer" }; writer.writeNext(header); return writer; }
Example 34
| Project: axelor-business-suite-master File: MetaGroupMenuAssistantService.java View source code |
public void createGroupMenuFile(MetaGroupMenuAssistant groupMenuAssistant) {
AppSettings appSettings = AppSettings.get();
File groupMenuFile = new File(appSettings.get("file.upload.dir"), getFileName(groupMenuAssistant));
try {
FileWriterWithEncoding fileWriter = new FileWriterWithEncoding(groupMenuFile, "utf-8");
CSVWriter csvWriter = new CSVWriter(fileWriter, ';');
List<String[]> rows = createHeader(groupMenuAssistant);
addMenuRows(groupMenuAssistant, rows);
csvWriter.writeAll(rows);
csvWriter.close();
createMetaFile(groupMenuFile, groupMenuAssistant);
} catch (Exception e) {
e.printStackTrace();
}
}Example 35
| Project: cointrader-master File: IoUtil.java View source code |
public static void outputCsv(TableOutput tableOutput, Writer out) {
final CSVWriter writer = new CSVWriter(out);
if (tableOutput == null)
return;
if (tableOutput.headers != null)
writer.writeNext(tableOutput.headers);
if (tableOutput.rows == null)
return;
for (String[] row : tableOutput.rows) writer.writeNext(row);
}Example 36
| Project: dkpro-csniper-master File: ExportCsvTask.java View source code |
@Override
protected void run() {
CSVWriter writer = null;
contextProvider.setOutputPos(exportModel.includePos);
outputFile = null;
try {
outputFile = File.createTempFile("date", ".csv");
writer = new CSVWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
List<AdditionalColumn> ac = exportModel.additionalColumns;
String[] row = new String[COLUMN_COUNT + ac.size()];
List<AggregatedEvaluationResult> results = repository.listAggregatedResults(formModel.getCollections(), formModel.getTypes(), formModel.getUsers(), formModel.getUserThreshold(), formModel.getConfidenceThreshold());
Collections.sort(results, new Comparator<AggregatedEvaluationResult>() {
@Override
public int compare(AggregatedEvaluationResult aO1, AggregatedEvaluationResult aO2) {
String id1 = aO1.getItem().getCollectionId() + "|" + aO1.getItem().getDocumentId();
String id2 = aO2.getItem().getCollectionId() + "|" + aO2.getItem().getDocumentId();
return id1.compareTo(id2);
}
});
// Write header row
row[0] = "User";
row[1] = "Collection";
row[2] = "Document";
row[3] = "Begin";
row[4] = "End";
row[5] = "Left";
row[6] = "Unit";
row[7] = "Right";
row[8] = "Type";
row[9] = "Class";
row[10] = "Confidence";
row[11] = "Correct";
row[12] = "Wrong";
for (int i = 0; i < ac.size(); i++) {
row[COLUMN_COUNT + i] = ac.get(i).getName();
}
writer.writeNext(row);
// Write rest
setTotal(results.size());
for (AggregatedEvaluationResult aer : results) {
ResultFilter classification = aer.getClassification();
if (formModel.getFilters().contains(classification)) {
ItemContext context = contextProvider.getContext(aer.getItem(), exportModel.contextSize, exportModel.contextSize);
// only differentiate between users if additional columns are being exported
Set<String> users;
if (ac.isEmpty()) {
users = new HashSet<String>(Arrays.asList(""));
} else {
users = aer.getUsers(false);
}
// columns entries might differ)
for (String user : users) {
row[0] = user;
row[1] = aer.getItem().getCollectionId();
row[2] = aer.getItem().getDocumentId();
row[3] = Long.toString(aer.getItem().getBeginOffset());
row[4] = Long.toString(aer.getItem().getEndOffset());
row[5] = context.getLeft();
row[6] = context.getUnit();
row[7] = context.getRight();
row[8] = aer.getItem().getType();
row[9] = classification.toString();
row[10] = Double.toString(aer.getConfidence());
row[11] = Integer.toString(aer.getCorrect());
row[12] = Integer.toString(aer.getWrong());
for (int i = 0; i < ac.size(); i++) {
row[COLUMN_COUNT + i] = repository.getEvaluationResult(aer.getItem().getId(), user).getAdditionalColumns().get(ac.get(i));
if (row[COLUMN_COUNT + i] == null) {
row[COLUMN_COUNT + i] = "";
}
}
writer.writeNext(row);
}
}
// Make sure we do not get to 100% before we did the classification, because
// otherwise ProgressBar.onFinish() will trigger!!!
increment();
if (isCancelled()) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
error("Export failed: " + ExceptionUtils.getRootCauseMessage(e));
cancel();
} finally {
IOUtils.closeQuietly(writer);
if (isCancelled()) {
clean();
}
}
}Example 37
| Project: edemocracia-master File: ExportacaoCSV.java View source code |
private static <T> void exportar(final Class<?> classe, final List<T> dados, final CSVWriter csvWriter) {
final Field[] campos = classe.getDeclaredFields();
final String[] colunas = new String[campos.length];
final Method[] metodosDeAcesso = new Method[campos.length];
// Coletando metodos de acesso get
for (int i = 0; i < campos.length; i++) {
final String nomeCampo = campos[i].getName();
colunas[i] = nomeCampo;
final String metodoDeAcesso = "get" + nomeCampo.substring(0, 1).toUpperCase() + nomeCampo.substring(1);
try {
metodosDeAcesso[i] = classe.getMethod(metodoDeAcesso, null);
} catch (SecurityException e) {
LOG.error("Erro ao exportar arquivo csv", e);
} catch (NoSuchMethodException e) {
LOG.error("Erro ao exportar arquivo csv. Método não encontrado.", e);
}
}
csvWriter.writeNext(colunas);
// Impressão dos dados
final Iterator<T> iteratorDados = dados.iterator();
final int columnCount = colunas.length;
while (iteratorDados.hasNext()) {
final T dadosForum = iteratorDados.next();
String[] nextLine = new String[columnCount];
for (int i = 0; i < columnCount; i++) {
try {
nextLine[i] = metodosDeAcesso[i].invoke(dadosForum, null) + "";
} catch (SecurityException e) {
LOG.error("Erro ao exportar arquivo csv", e);
} catch (IllegalArgumentException e) {
LOG.error("Erro ao exportar arquivo csv", e);
} catch (IllegalAccessException e) {
LOG.error("Erro ao exportar arquivo csv", e);
} catch (InvocationTargetException e) {
LOG.error("Erro ao exportar arquivo csv", e);
}
}
csvWriter.writeNext(nextLine);
}
}Example 38
| Project: EEGReader-master File: DatabaseHandler.java View source code |
public boolean exportTableToCSVFile(Cursor csvCursor, String fileName) {
File exportDir = new File(Environment.getExternalStorageDirectory(), "");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File file = new File(exportDir, fileName);
try {
file.createNewFile();
CSVWriter csvWriter = new CSVWriter(new FileWriter(file));
csvWriter.writeNext(csvCursor.getColumnNames());
if (csvCursor.getCount() > 0) {
while (csvCursor.moveToNext()) {
String arrayOfStrings[] = { csvCursor.getString(0), csvCursor.getString(1), csvCursor.getString(2), csvCursor.getString(3) };
csvWriter.writeNext(arrayOfStrings);
}
} else {
return false;
}
csvWriter.close();
csvCursor.close();
return true;
} catch (SQLException sqlException) {
LOGE(TAG, sqlException.getMessage(), sqlException);
return false;
} catch (IOException ioException) {
LOGE(TAG, ioException.getMessage(), ioException);
return false;
}
}Example 39
| Project: graylog2-server-master File: SearchResponseCsvWriter.java View source code |
@Override
public void writeTo(SearchResponse searchResponse, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
final CSVWriter csvWriter = new CSVWriter(new OutputStreamWriter(entityStream, StandardCharsets.UTF_8));
final ImmutableSortedSet<String> sortedFields = ImmutableSortedSet.copyOf(Iterables.concat(searchResponse.fields(), Lists.newArrayList(Message.FIELD_SOURCE, Message.FIELD_TIMESTAMP)));
// write field headers
csvWriter.writeNext(sortedFields.toArray(new String[sortedFields.size()]));
// write result set in same order as the header row
final String[] fieldValues = new String[sortedFields.size()];
for (ResultMessageSummary message : searchResponse.messages()) {
int idx = 0;
// first collect all values from the current message
for (String fieldName : sortedFields) {
final Object val = message.message().get(fieldName);
fieldValues[idx++] = ((val == null) ? null : val.toString().replaceAll("\n", "\\\\n"));
fieldValues[idx++] = ((val == null) ? null : val.toString().replaceAll("\r", "\\\\r"));
}
// write the complete line, some fields might not be present in the message, so there might be null values
csvWriter.writeNext(fieldValues);
}
if (csvWriter.checkError()) {
LOG.error("Encountered unspecified error when writing message result as CSV, result is likely malformed.");
}
csvWriter.close();
}Example 40
| Project: java-aws-mturk-master File: HITDataCSVWriter.java View source code |
protected synchronized CSVWriter getWriter() throws IOException { if (this.csvWriter == null) { // always append, FileOutputStream fos = new FileOutputStream(this.fileName, appendToFile); OutputStreamWriter writer = new OutputStreamWriter(fos, "UTF-8"); this.csvWriter = new CSVWriter(writer, separator, useQuotes ? CSVWriter.DEFAULT_QUOTE_CHARACTER : CSVWriter.NO_QUOTE_CHARACTER, System.getProperty("line.separator")); } return this.csvWriter; }
Example 41
| Project: jBilling-master File: CsvExporter.java View source code |
public String export(List<? extends Exportable> list) {
String[] header;
// extract the field names for the CSV header
try {
header = type.newInstance().getFieldNames();
} catch (InstantiationException e) {
LOG.debug("Could not produce a new instance of " + type.getSimpleName() + " to build CSV header.");
return null;
} catch (IllegalAccessException e) {
LOG.debug("Constructor of " + type.getSimpleName() + " is not accessible to build CSV header.");
return null;
}
StringWriter out = new StringWriter();
CSVWriter writer = new CSVWriter(out);
writer.writeNext(header);
for (Exportable exportable : list) {
for (Object[] values : exportable.getFieldValues()) {
writer.writeNext(convertToString(values));
}
}
try {
writer.close();
out.close();
} catch (IOException e) {
LOG.debug("Writer cannot be closed, exported CSV may be missing data.");
}
return out.toString();
}Example 42
| Project: jsfexporter-master File: CSVExportType.java View source code |
@Override
public void writeExport(ExternalContext externalContext) throws Exception {
String encoding = configOptions.getCharacterEncoding();
boolean writeBOM = false;
if ("UTF-8-with-bom".equalsIgnoreCase(encoding)) {
externalContext.setResponseCharacterEncoding("UTF-8");
writeBOM = true;
} else {
externalContext.setResponseCharacterEncoding(encoding);
}
Writer outputWriter = externalContext.getResponseOutputWriter();
CSVWriter csvWriter = new CSVWriter(outputWriter, configOptions.getSeparatorCharacter(), configOptions.getQuoteCharacter(), configOptions.getEscapeCharacter(), configOptions.getLineTerminator());
if (writeBOM) {
outputWriter.write('');
}
for (List<String> row : rows) {
csvWriter.writeNext(row.toArray(EMPTY_STRING_ARRAY));
}
}Example 43
| Project: jspringbot-libraries-master File: TestDataResourceState.java View source code |
public String getCSVContentHeader(int index) {
TestData data = getTestData(index);
List<String> contentHeader = new LinkedList<String>();
for (int i = data.getHeaderColumnSize(); i < headerLine.size(); i++) {
contentHeader.add(headerLine.get(i));
}
StringWriter str = new StringWriter();
CSVWriter writer = new CSVWriter(str);
try {
writer.writeNext(contentHeader.toArray(new String[contentHeader.size()]));
} finally {
IOUtils.closeQuietly(writer);
IOUtils.closeQuietly(str);
}
return str.toString();
}Example 44
| Project: korok-master File: EntrantExporter.java View source code |
private String generateCSV(List<EntrantExportLine> lines) {
StringWriter strWriter = new StringWriter();
CSVWriter writer = new CSVWriter(strWriter);
writer.writeNext(headers);
int count = 0;
for (EntrantExportLine line : lines) {
if (line.getEntrantNumber() >= minEntrantNumber) {
++count;
writer.writeNext(line.getFields());
}
}
logger.info("Exported {} entrants for (semester, type, min) ({}, {}, {})", count, semester, entrantType, minEntrantNumber);
return strWriter.toString();
}Example 45
| Project: Leboncoin-master File: Persistence.java View source code |
private synchronized boolean persist(String id, String hash, boolean append) throws IOException {
// Append
CSVReader reader = new CSVReader(new FileReader(NameFactory.PERSISTENCE_CSV));
List<String[]> allEntries = reader.readAll();
for (String[] entry : allEntries) {
if (entry[0].equals(hash)) {
reader.close();
return false;
}
}
reader.close();
CSVWriter writer = new CSVWriter(new FileWriter(NameFactory.PERSISTENCE_CSV, append));
String[] entries = new String[2];
entries[0] = hash;
entries[1] = id;
writer.writeNext(entries);
writer.close();
return true;
}Example 46
| Project: MOCBuilder-master File: CSVExport.java View source code |
public static void main(String[] args) throws Exception {
Configuration configuration = new Configuration();
OrderHelper helper = new OrderHelper(configuration.getProperty(ConfigurationProperty.COMMENT_REGEX));
BrickLinkClient client = new BrickLinkClient(configuration);
List<String[]> output = new ArrayList<String[]>();
try {
OrdersRequest request = new OrdersRequest();
OrdersResponse response = client.execute(request);
List<OrderDT> orders = response.getOrders();
Collections.sort(orders, Collections.reverseOrder(new Comparator<OrderDT>() {
@Override
public int compare(OrderDT o1, OrderDT o2) {
return Integer.valueOf(o1.getId()).compareTo(Integer.valueOf(o2.getId()));
}
}));
for (OrderDT tmp : orders) {
OrderDT order = client.execute(new OrderRequest(tmp.getId())).getOrder();
if (helper.isNotBilled(order)) {
AddressDT address = order.getShipping().getAddress();
NameDT name = address.getName();
String email = order.getEmail();
List<String> parts = new ArrayList<String>();
parts.add(order.getBuyer());
String fullName = name.getFull();
int lastNamePos = fullName.lastIndexOf(' ');
parts.add(fullName.substring(0, lastNamePos));
parts.add(fullName.substring(lastNamePos + 1));
parts.add(email);
String[] fullAddress = address.getFull().split("\r\n");
for (String addressPart : fullAddress) {
parts.add(addressPart);
}
parts.add(address.getCountry().name());
output.add(parts.toArray(new String[parts.size()]));
} else {
break;
}
}
} finally {
client.close();
}
FileOutputStream fis = null;
OutputStreamWriter osw = null;
CSVWriter csvw = null;
try {
fis = new FileOutputStream(new File("kunden.csv"));
osw = new OutputStreamWriter(fis, "Windows-1252");
csvw = new CSVWriter(osw, ';', CSVWriter.DEFAULT_QUOTE_CHARACTER, "\r\n");
csvw.writeAll(output);
} finally {
IOUtils.closeQuietly(osw);
IOUtils.closeQuietly(fis);
if (null != csvw) {
csvw.close();
}
}
}Example 47
| Project: netlib-java-master File: Benchmarks.java View source code |
public static void main(String[] args) throws Exception {
int reps = Integer.valueOf(getProperty("perf.reps", "10"));
int sets = Integer.valueOf(getProperty("perf.max", "100"));
// make sure we're all loaded
BLAS.getInstance();
LAPACK.getInstance();
ARPACK.getInstance();
List<Benchmark> benchmarks = Lists.newArrayList();
// benchmarks.add(new Linpack());
List<Benchmark.Parameterised> pBenchmarks = Lists.newArrayList();
pBenchmarks.add(new Ddot());
pBenchmarks.add(new Dgemm());
pBenchmarks.add(new Dgetri());
pBenchmarks.add(new Dsaupd());
for (Benchmark b : benchmarks) {
File file = new File(getTarget(b) + ".csv");
log.info("writing to " + file);
@Cleanup CSVWriter csv = new CSVWriter(new FileWriter(file));
for (int i = 0; i < sets; i++) {
long result = b.benchmark();
csv.writeNext(new String[] { Long.toString(result) });
}
}
double factor = 6 / 100.0;
for (Benchmark.Parameterised b : pBenchmarks) {
File file = new File(getTarget(b) + ".csv");
log.info("writing to " + file);
@Cleanup CSVWriter csv = new CSVWriter(new FileWriter(file));
for (int i = 0; i < reps; i++) {
log.info(file + " rep " + i);
for (int j = sets; j > 0; j--) {
int size = (int) Math.pow(10, factor * j);
if (size < 10)
continue;
try {
long result = b.benchmark(size);
csv.writeNext(new String[] { Integer.toString(size), Long.toString(result) });
} catch (IllegalArgumentException e) {
log.info("skipping a datum...");
}
}
}
}
}Example 48
| Project: opennaas-master File: CircuitStatisticsCapability.java View source code |
private String writeToCSV(SortedMap<Long, List<CircuitStatistics>> circuitStatistics) throws IOException {
StringBuilder sb = new StringBuilder();
CSVWriter writer = new CSVWriter(new StringBuilderWriter(sb), CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER);
try {
for (Long timestamp : circuitStatistics.keySet()) for (CircuitStatistics currentStatistic : circuitStatistics.get(timestamp)) {
String[] csvStatistic = new String[7];
csvStatistic[0] = String.valueOf(timestamp);
csvStatistic[1] = currentStatistic.getSlaFlowId();
csvStatistic[2] = currentStatistic.getThroughput();
csvStatistic[3] = currentStatistic.getPacketLoss();
csvStatistic[4] = currentStatistic.getDelay();
csvStatistic[5] = currentStatistic.getJitter();
csvStatistic[6] = currentStatistic.getFlowData();
writer.writeNext(csvStatistic);
}
return sb.toString();
} finally {
writer.close();
}
}Example 49
| Project: performance-test-harness-for-geoevent-master File: CSVReportWriter.java View source code |
@Override
public void writeReport(String reportFile, List<String> testNames, List<String> columnNames, Map<String, List<FixtureStatistic>> stats) throws IOException {
CSVWriter reportWriter = null;
try {
//create the parent directories - if needed
createParentDirectoriesIfNeeded(reportFile);
//rollover the file - keep backups
rollOver(reportFile);
//init the report writer
reportWriter = new CSVWriter(new FileWriter(reportFile), ',');
//copy the column names - add the test name as the first column
List<String> columnNamesCopy = new ArrayList<String>();
columnNamesCopy.add("Test Name");
columnNamesCopy.addAll(columnNames);
//write the headers
reportWriter.writeNext(columnNamesCopy.toArray(new String[columnNamesCopy.size()]));
reportWriter.flush();
//int index = 0;
for (String testName : testNames) {
//insert an empty line break
// if( index > 0 )
// reportWriter.writeNext(new String[]{""});
// get each test's fixture stats and sort them accordingly
List<FixtureStatistic> fixtureStats = stats.get(testName);
if (fixtureStats == null || fixtureStats.size() == 0) {
continue;
}
Collections.sort(fixtureStats);
for (FixtureStatistic fixtureStat : fixtureStats) {
reportWriter.writeNext(writeRow(testName, columnNames, fixtureStat));
reportWriter.flush();
}
//index++;
}
//write out the total time
if (getTotalTestingTime() != -1) {
reportWriter.writeNext(new String[] { "" });
reportWriter.writeNext(new String[] { "" });
reportWriter.writeNext(new String[] { "Total Testing Time: " + formatTime(getTotalTestingTime()) });
}
} finally {
if (reportWriter != null) {
reportWriter.flush();
}
IOUtils.closeQuietly(reportWriter);
}
}Example 50
| Project: SeavusJB3-master File: CsvExporter.java View source code |
public String export(List<? extends Exportable> list) {
String[] header;
// extract the field names for the CSV header
try {
header = type.newInstance().getFieldNames();
} catch (InstantiationException e) {
LOG.debug("Could not produce a new instance of " + type.getSimpleName() + " to build CSV header.");
return null;
} catch (IllegalAccessException e) {
LOG.debug("Constructor of " + type.getSimpleName() + " is not accessible to build CSV header.");
return null;
}
StringWriter out = new StringWriter();
CSVWriter writer = new CSVWriter(out);
writer.writeNext(header);
for (Exportable exportable : list) {
for (Object[] values : exportable.getFieldValues()) {
writer.writeNext(convertToString(values));
}
}
try {
writer.close();
out.close();
} catch (IOException e) {
LOG.debug("Writer cannot be closed, exported CSV may be missing data.");
}
return out.toString();
}Example 51
| Project: simelec-master File: LoadModel.java View source code |
/**
* Writes the results of this LoadModel to a specified File
*
* @param file
* the file on which to write the results
* @throws IOException
* if there are problems writing the results to file
*/
private void writeResults(File file) throws IOException {
/*
* Calculate the totals. We do this even if the total flag
* isn't set because someone might ask for the grand total.
*/
totalConsumption = new // W
double[// W
1440];
for (Load l : loads) {
for (int i = 0; i < totalConsumption.length; i++) {
totalConsumption[i] += l.getConsumption(i + 1);
}
}
// Write the data back to the simulation sheet
ArrayList<String[]> results;
if (totalOnly) {
results = new ArrayList<String[]>(1);
String label = this.getClass().getSimpleName().replaceFirst("Model", "");
results.add(Load.buildExportString(label.toUpperCase(), totalConsumption));
} else {
results = new ArrayList<String[]>(loads.size());
for (Load a : loads) {
results.add(a.toExportString());
}
}
// Write the data to a file
CSVWriter writer = new CSVWriter(new FileWriter(file), ',', '\0');
writer.writeAll(results);
writer.close();
}Example 52
| Project: sisob-academic-data-extractor-master File: CrawlerTrace.java View source code |
public void close() {
for (String key : urls_trace_files.keySet()) {
CSVWriter writer = this.urls_trace_files.get(key);
if (writer != null) {
try {
writer.close();
} catch (IOException ex) {
ProjectLogger.LOGGER.error(ex.getMessage(), ex);
}
urls_trace_files.put(key, null);
}
}
urls_trace_files.clear();
urls_trace_files = null;
}Example 53
| Project: sqlpower-library-master File: TableModelCSVFormatter.java View source code |
/**
* Outputs a table in HTML that represents the TableModel. Only the rows
* given will be written to the file.
*/
public void formatToStream(TableModel model, PrintWriter writer, int[] selectedRows) {
try {
CSVWriter csvWriter = new CSVWriter(writer);
List<String[]> tableExport = new ArrayList<String[]>();
int columnCount = model.getColumnCount();
String[] rowArray = new String[columnCount];
for (int i = 0; i < columnCount; i++) {
rowArray[i] = model.getColumnName(i);
}
tableExport.add(rowArray);
for (int row = 0; row < selectedRows.length; row++) {
rowArray = new String[columnCount];
for (int col = 0; col < columnCount; col++) {
Object value = model.getValueAt(selectedRows[row], col);
if (value != null) {
if (columnFormatters.get(col) != null) {
rowArray[col] = columnFormatters.get(col).format(value);
} else {
rowArray[col] = value.toString();
}
} else {
rowArray[col] = "";
}
}
tableExport.add(rowArray);
}
csvWriter.writeAll(tableExport);
csvWriter.close();
} catch (IOException ex) {
throw new RuntimeException("Could not close the CSV Writer", ex);
}
}Example 54
| Project: szeke-master File: Data2Features.java View source code |
// convert the csv file to arff file
// return the fpath of arff file
/*public static void Data2arff(String csvpath, String arfpath) {
try {
CSVLoader loader = new CSVLoader();
loader.setSource(new File(csvpath));
Instances data = loader.getDataSet();
// save ARFF
ArffSaver saver = new ArffSaver();
saver.setInstances(data);
saver.setFile(new File(arfpath));
saver.setDestination(new File(arfpath));
saver.writeBatch();
} catch (Exception ex) {
ex.printStackTrace();
}
}*/
public static void Testdata2CSV(Vector<String> tests, String fpath, RecordFeatureSet rf) {
try {
CSVWriter writer = new CSVWriter(new FileWriter(new File(fpath)));
// get attribute names
// get attribute names
Collection<String> attrStrings = rf.getFeatureNames();
String[] attr_names = attrStrings.toArray(new String[attrStrings.size() + 1]);
attr_names[attr_names.length - 1] = "label";
writer.writeNext(attr_names);
for (String Record : tests) {
Vector<String> row = new Vector<String>();
Collection<Feature> cf = rf.computeFeatures(Record, "");
Feature[] x = cf.toArray(new Feature[cf.size()]);
// row.add(f.getName());
for (int k = 0; k < cf.size(); k++) {
row.add(String.valueOf(x[k].getScore()));
}
row.add("");
String[] dataEntry = row.toArray(new String[row.size()]);
writer.writeNext(dataEntry);
}
writer.flush();
writer.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}Example 55
| Project: thucydides-master File: CSVReporter.java View source code |
private void writeTitleRow(CSVWriter writer) {
Inflector inflector = Inflector.getInstance();
List<String> titles = new ArrayList<String>();
titles.addAll(Arrays.asList(TITLE_LINE));
for (String extraColumn : extraColumns) {
titles.add(inflector.of(extraColumn).asATitle().toString());
}
writer.writeNext(titles.toArray(OF_STRINGS));
}Example 56
| Project: TracInstant-master File: TabTicketWriter.java View source code |
public static void write(Writer writer, Set<String> fieldsSet, List<Ticket> tickets) {
List<String> fields = new ArrayList<>();
fields.add("id");
fields.addAll(fieldsSet);
CSVWriter csvWriter = new CSVWriter(writer, '\t');
csvWriter.writeNext(fields.toArray(new String[0]));
for (Ticket subTicket : tickets) {
String[] values = new String[fields.size()];
// TODO: Should the table model just expose the field via '#'?
// (It's internally stored as an int *mainly* so that JTable renders
// it right-aligned! But publically, it should be a searchable String field.)
values[0] = Integer.toString(subTicket.getNumber());
int i = 1;
for (String f : fieldsSet) {
values[i++] = subTicket.getValue(f);
}
csvWriter.writeNext(values);
}
try {
csvWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}Example 57
| Project: aifh-master File: DataUtil.java View source code |
/**
* Dump a dataset as a CSV.
* @param file The file to dump to.
* @param dataset The dataset.
* @throws IOException If an IO error occurs.
*/
public static void dumpCSV(File file, List<BasicData> dataset) throws IOException {
CSVWriter writer = new CSVWriter(new FileWriter(file));
int inputCount = dataset.get(0).getInput().length;
int outputCount = dataset.get(0).getIdeal().length;
int totalCount = inputCount + outputCount;
String[] headers = new String[totalCount];
int idx = 0;
for (int i = 0; i < inputCount; i++) {
headers[idx++] = "x" + i;
}
for (int i = 0; i < outputCount; i++) {
headers[idx++] = "y" + i;
}
writer.writeNext(headers);
String[] line = new String[totalCount];
for (int i = 0; i < dataset.size(); i++) {
BasicData item = dataset.get(i);
idx = 0;
for (int j = 0; j < inputCount; j++) {
line[idx++] = String.format(Locale.ENGLISH, "%.2f", item.getInput()[j]);
}
for (int j = 0; j < outputCount; j++) {
line[idx++] = String.format(Locale.ENGLISH, "%.2f", item.getIdeal()[j]);
}
writer.writeNext(line);
}
writer.close();
}Example 58
| Project: ANNIS-master File: FrequencyResultPanel.java View source code |
@Override
public InputStream getStream() {
try {
File tmpFile = File.createTempFile("annis-frequency", ".txt");
tmpFile.deleteOnExit();
try (Writer writer = new OutputStreamWriter(new FileOutputStream(tmpFile), Charsets.UTF_8)) {
CSVWriter csv = new CSVWriter(writer, '\t', CSVWriter.NO_QUOTE_CHARACTER, '\\');
ArrayList<String> header = new ArrayList<>();
if (data.getEntries().size() > 0) {
for (int i = 0; i < data.getEntries().iterator().next().getTupel().length; i++) {
FrequencyTableEntry e = freqDefinition.get(i);
header.add(getCaption(e));
}
}
header.add("count");
csv.writeNext(header.toArray(new String[0]));
for (FrequencyTable.Entry e : data.getEntries()) {
ArrayList<String> d = new ArrayList<>();
d.addAll(Arrays.asList(e.getTupel()));
d.add("" + e.getCount());
csv.writeNext(d.toArray(new String[0]));
}
}
return new FileInputStream(tmpFile);
} catch (IOException ex) {
log.error(null, ex);
}
return new ByteArrayInputStream(new byte[0]);
}Example 59
| Project: as-spacebar-master File: PreferenceInitializer.java View source code |
@Override
public void initializeDefaultPreferences() {
IPreferenceStore store = SpaceBarPlugin.getDefault().getPreferenceStore();
store.setDefault(Preferences.EXPORT_BATCH_SIZE, 1000);
store.setDefault(Preferences.EXPORT_WORKER_COUNT, 1);
store.setDefault(Preferences.EXPORT_TIME_SCOPE, "CURRENT");
store.setDefault(Preferences.EXPORT_PREFETCH, 1000L);
store.setDefault(Preferences.EXPORT_QUERY_LIMIT, 100000L);
store.setDefault(Preferences.EXPORT_TIMEOUT, -1L);
store.setDefault(Preferences.SPACE_EDITOR_CLIPBOARD_SEPARATOR, "\\t");
store.setDefault(Preferences.SPACE_EDITOR_CLIPBOARD_HEADER, false);
store.setDefault(Preferences.EXPORT_CSV_HEADER, true);
store.setDefault(Preferences.EXPORT_CSV_SEPARATOR, String.valueOf(CSVWriter.DEFAULT_SEPARATOR));
// not a bug, import and export have to use the same default escape
// character
store.setDefault(Preferences.EXPORT_CSV_ESCAPE_CHARACTER, String.valueOf(CSVParser.DEFAULT_ESCAPE_CHARACTER));
store.setDefault(Preferences.EXPORT_CSV_QUOTE_CHARACTER, String.valueOf(CSVWriter.DEFAULT_QUOTE_CHARACTER));
store.setDefault(Preferences.EXPORT_CSV_FORMAT_BLOB, Blob.HEX.name());
store.setDefault(Preferences.EXPORT_CSV_FORMAT_BOOLEAN, ConverterFactory.DEFAULT_PATTERN_BOOLEAN);
// store.setDefault(Preferences.EXPORT_CSV_FORMAT_DATE,
// ConverterFactory.DEFAULT_PATTERN_DATE);
store.setDefault(Preferences.EXPORT_EXCEL_HEADER, true);
store.setDefault(Preferences.EXPORT_EXCEL_VERSION, SpreadsheetVersion.EXCEL2007.name());
store.setDefault(Preferences.EXPORT_EXCEL_FORMAT_BLOB, Blob.HEX.name());
store.setDefault(Preferences.EXPORT_EXCEL_FORMAT_DATE, ExcelExporter.DEFAULT_DATETIME_PATTERN);
store.setDefault(Preferences.IMPORT_BATCH_SIZE, 1000);
store.setDefault(Preferences.IMPORT_WORKER_COUNT, 1);
store.setDefault(Preferences.IMPORT_OPERATION, "PUT");
store.setDefault(Preferences.IMPORT_DISTRIBUTION_ROLE, "LEECH");
store.setDefault(Preferences.IMPORT_WAIT_FOR_READY_TIMEOUT, 30000L);
store.setDefault(Preferences.IMPORT_CSV_HEADER, true);
store.setDefault(Preferences.IMPORT_CSV_ESCAPE_CHARACTER, String.valueOf(CSVParser.DEFAULT_ESCAPE_CHARACTER));
store.setDefault(Preferences.IMPORT_CSV_IGNORE_LEADING_WHITE_SPACE, CSVParser.DEFAULT_IGNORE_LEADING_WHITESPACE);
store.setDefault(Preferences.IMPORT_CSV_QUOTE_CHARACTER, String.valueOf(CSVParser.DEFAULT_QUOTE_CHARACTER));
store.setDefault(Preferences.IMPORT_CSV_SEPARATOR, String.valueOf(CSVParser.DEFAULT_SEPARATOR));
store.setDefault(Preferences.IMPORT_CSV_STRICT_QUOTES, CSVParser.DEFAULT_STRICT_QUOTES);
store.setDefault(Preferences.IMPORT_CSV_FORMAT_BLOB, Blob.HEX.name());
store.setDefault(Preferences.IMPORT_CSV_FORMAT_BOOLEAN, ConverterFactory.DEFAULT_PATTERN_BOOLEAN);
// store.setDefault(Preferences.IMPORT_CSV_FORMAT_DATE,
// ConverterFactory.DEFAULT_PATTERN_DATE);
store.setDefault(Preferences.IMPORT_EXCEL_HEADER, true);
store.setDefault(Preferences.IMPORT_EXCEL_FORMAT_BLOB, Blob.HEX.name());
store.setDefault(Preferences.SPACE_EDITOR_THEME, Preferences.THEME_MODERN);
PreferenceConverter.setDefault(store, Preferences.SPACE_EDITOR_COLOR_BLINK, new RGB(255, 204, 0));
PreferenceConverter.setDefault(store, Preferences.SPACE_EDITOR_COLOR_BLINK_UP, new RGB(0, 204, 0));
PreferenceConverter.setDefault(store, Preferences.SPACE_EDITOR_COLOR_BLINK_DOWN, new RGB(204, 0, 0));
// store.setDefault(Preferences.SPACE_EDITOR_DATE_FORMAT,
// ConverterFactory.DEFAULT_PATTERN_DATE);
store.setDefault(Preferences.SPACE_EDITOR_TIME_ZONE, TimeZone.getTimeZone("GMT").getID());
store.setDefault(Preferences.SPACE_EDITOR_BROWSE_TIME_SCOPE, "CURRENT");
store.setDefault(Preferences.SPACE_EDITOR_BROWSE_LIMIT, 10000L);
setBrowseDefaults(store, Preferences.TIMESCOPE_ALL, 1000L, 10000L, -1L);
setBrowseDefaults(store, Preferences.TIMESCOPE_NEW, null, null, -1L);
setBrowseDefaults(store, Preferences.TIMESCOPE_SNAPSHOT, 1000L, 100000L, null);
setBrowseDefaults(store, Preferences.TIMESCOPE_CURRENT, 1000L, null, null);
}Example 60
| Project: Curators-Workbench-master File: CSVArrangementExportJob.java View source code |
/*
* (non-Javadoc)
*
* @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime. IProgressMonitor)
*/
@Override
protected IStatus run(IProgressMonitor monitor) {
if (monitor == null) {
monitor = new NullProgressMonitor();
}
MetsProjectNature n = null;
try {
n = (MetsProjectNature) p.getNature(MetsProjectNature.NATURE_ID);
} catch (CoreException e) {
return new Status(Status.ERROR, Activator.PLUGIN_ID, "There was a problem obtaining the METS project nature.", e);
}
MetsType1 workbench = n.getMets();
DivType bag = METSUtils.findBagDiv(workbench);
// setup CSV file handler
CSVWriter writer = null;
try {
writer = new CSVWriter(new FileWriter(filepath));
writer.writeNext(new String[] { "order", "depth", "pid", "label", "MODS title" });
int order = 1;
for (TreeIterator<EObject> iter = bag.eAllContents(); iter.hasNext(); ) {
EObject eo = iter.next();
if (eo instanceof DivType) {
DivType d = (DivType) eo;
String uuid = METSUtils.getUUID(d);
int depth = METSUtils.getDepth(d);
String title = getTitle(d);
writer.writeNext(new String[] { String.valueOf(order), String.valueOf(depth), uuid, d.getLABEL1(), title != null ? title : "" });
order++;
}
}
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Cannot open the export file: " + filepath);
}
try {
p.refreshLocal(IResource.DEPTH_ONE, new NullProgressMonitor());
} catch (CoreException e) {
e.printStackTrace();
}
return Status.OK_STATUS;
}Example 61
| Project: Excel-to-Anki-master File: AnkiConverter.java View source code |
/**
* Parses the S for pairs and adds they to the W.
*/
private final void createCSVFromXSL(CSVWriter w, Sheet s) {
final int rows = s.getRows();
final int columns = s.getColumns();
if ((rows <= 1) || (columns == 0)) {
setStatus("The input file does not has a content!");
return;
}
/**
* Create a content for the CSV file for the Anki.
*/
for (int i = 1; i < rows; i++) {
String[] entries = new String[2];
Cell c = s.getCell(0, i);
entries[0] = c.getContents();
c = s.getCell(1, i);
entries[1] = c.getContents();
w.writeNext(entries);
}
addStatus("Converted " + (rows - 1) + " pairs.");
}Example 62
| Project: GraphIt-master File: LastFMGraphReadWriteLoadTest.java View source code |
private static void createScenario(PropertyGraph graph, String fileName, int readPercentage, int removePercentage, int addPercentage) throws IOException {
Preconditions.checkArgument(readPercentage + removePercentage + addPercentage == 100);
Random random = new Random(1234);
List<Edge> edges = graph.getEdges().filter(new EdgeTypeFilter(LastFMGraph.LISTENED_TO)).asList();
Collections.shuffle(edges, random);
List<Node> nodes = graph.getNodes().asList();
Collections.shuffle(nodes, random);
List<String[]> rows = new ArrayList<String[]>();
List<Node> users = graph.getNodes().filter(new NodeTypeFilter(LastFMGraph.USER)).asList();
List<Node> artists = graph.getNodes().filter(new NodeTypeFilter(LastFMGraph.ARTIST)).asList();
int i = 0;
for (Edge edge : edges) {
Node node = edge.getStartNode();
int j = i % 100;
if (j < readPercentage) {
EdgeDirection edgeDirection = node.getType().equals(LastFMGraph.USER) ? EdgeDirection.OUTGOING : EdgeDirection.INCOMING;
rows.add(new String[] { "GET_EDGES", node.getType().name(), node.getNodeId().getId(), LastFMGraph.LISTENED_TO.name(), edgeDirection.name() });
} else if (j < readPercentage + removePercentage) {
// Remove random edge
rows.add(new String[] { "REMOVE_EDGE", edge.getType().name(), edge.getIndex() + "" });
} else {
// Add random edge
Node user = users.get(Math.abs(random.nextInt()) % users.size());
Node artist = artists.get(Math.abs(random.nextInt()) % artists.size());
rows.add(new String[] { "ADD_EDGE", LastFMGraph.USER.name(), user.getNodeId().getId(), LastFMGraph.ARTIST.name(), artist.getNodeId().getId(), LastFMGraph.LISTENED_TO.name(), "100" });
}
i++;
}
Collections.shuffle(rows, random);
CSVWriter writer = null;
try {
writer = new CSVWriter(new FileWriter(fileName));
writer.writeAll(rows);
} finally {
IOUtils.closeQuietly(writer);
}
}Example 63
| Project: h-store-master File: VoltTableUtil.java View source code |
/**
* Dump out a VoltTable as a CSV to the given writer
* If the header flag is set to true, then the output will include
* the column names in the first row
* @param out
* @param vt
* @param write_header
*/
public static void csv(Writer out, VoltTable vt, boolean header) {
CSVWriter writer = new CSVWriter(out);
if (header) {
String cols[] = new String[vt.getColumnCount()];
for (int i = 0; i < cols.length; i++) {
cols[i] = vt.getColumnName(i);
}
// FOR
writer.writeNext(cols);
}
vt.resetRowPosition();
while (vt.advanceRow()) {
String row[] = vt.getRowStringArray();
assert (row != null);
assert (row.length == vt.getColumnCount());
writer.writeNext(row);
}
// WHILE
}Example 64
| Project: jmetrik-master File: DerbyDelimitedFileExporter.java View source code |
public void exportDelimitedFile() throws IOException, SQLException {
try {
JmetrikCsvWriter writer = null;
if (useQuotes) {
writer = new JmetrikCsvWriter(new FileWriter(new File(outputFileName)), delimiter);
} else {
writer = new JmetrikCsvWriter(new FileWriter(new File(outputFileName)), delimiter, CSVWriter.NO_QUOTE_CHARACTER);
}
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery("SELECT * FROM " + tableName.getNameForDatabase());
//write data
if (header) {
writer.writeHeader(variables);
}
if (exportScoredData) {
writer.writeScoredDatabase(rs, variables);
} else {
writer.writeDatabase(rs, variables);
}
rs.close();
stmt.close();
writer.close();
} catch (IOException ex) {
logger.fatal(ex.getMessage(), ex);
throw new IOException(ex);
} catch (SQLException ex) {
logger.fatal(ex.getMessage(), ex);
throw new SQLException(ex);
} catch (IllegalArgumentException ex) {
logger.fatal(ex.getMessage(), ex);
throw new IllegalArgumentException(ex);
}
}Example 65
| Project: medsavant-master File: ExportTable.java View source code |
private static void exportCSV(File file, JTable table) throws IOException {
// setup file
BufferedWriter writer = new BufferedWriter(new FileWriter(file, false));
CSVWriter out = new CSVWriter(writer, ',', '"');
// add headers
String[] header = new String[table.getColumnCount()];
for (int j = 0; j < table.getColumnCount(); j++) {
header[j] = table.getColumnName(j);
}
out.writeNext(header);
// add cells
for (int i = 0; i < table.getRowCount(); i++) {
String[] row = new String[table.getColumnCount()];
for (int j = 0; j < table.getColumnCount(); j++) {
row[j] = getString(table.getValueAt(i, j));
}
out.writeNext(row);
}
// close output
out.close();
writer.close();
}Example 66
| Project: MulCal-master File: History.java View source code |
public void Save(String fileName) throws IOException {
CSVWriter writer = new CSVWriter(new FileWriter(fileName), ',');
try {
for (HistoryItem item : this.items.values()) {
String[] entries = new String[4];
item.SanityCheck();
entries[0] = item.id;
entries[1] = item.equation;
entries[2] = item.result.toPlainString();
entries[3] = item.comment;
writer.writeNext(entries);
}
} finally {
writer.close();
}
}Example 67
| Project: NFC-Lottery-master File: DBExporter.java View source code |
/**
* Exports the DB contents in CSV format to external storage.
*
* @param context The current context
*
* @return Returns the output file path, or <code>null</code> if something
* went wrong during the export process.
*/
public static String exportDB(Context context) {
if (DEBUG)
Log.d(TAG, "Exporting DB contents to CSV file");
if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
Log.e(TAG, "Unable to export the DB. External storage is not available.");
return null;
}
// Ensure the export directory exists
File exportDir = new File(Environment.getExternalStorageDirectory(), FOLDER_NAME);
if (!exportDir.exists()) {
if (!exportDir.mkdirs()) {
Log.w(TAG, "Impossible to create the dirs path: " + exportDir.getAbsolutePath());
// We'll try anyway to export the DB, you never know.
}
}
final String fileName = String.format("%s%s%s", FILENAME_PREFIX, DateFormat.format(TIMESTAMP_FORMAT, System.currentTimeMillis()), FILENAME_EXTENSION);
File file = new File(exportDir, fileName);
if (DEBUG)
Log.v(TAG, "CSV file DB dump target path: " + file.getAbsolutePath());
// Read ALL the rows!
ContentResolver cr = context.getContentResolver();
Cursor curCSV = cr.query(NFCMLContent.Geeks.CONTENT_URI, null, null, null, null);
CSVWriter csvWrite = null;
try {
// Create the new file, then begin writing the contents to it.
file.createNewFile();
csvWrite = new CSVWriter(new FileWriter(file));
// Writes the column headers
csvWrite.writeNext(curCSV.getColumnNames());
while (curCSV.moveToNext()) {
// All the columns to export
String arrStr[] = { curCSV.getString(NFCMLContent.Geeks.Columns.ID.getIndex()), curCSV.getString(NFCMLContent.Geeks.Columns.EMAIL.getIndex()), curCSV.getString(NFCMLContent.Geeks.Columns.NAME.getIndex()), curCSV.getString(NFCMLContent.Geeks.Columns.ORGANIZATION.getIndex()), curCSV.getString(NFCMLContent.Geeks.Columns.TITLE.getIndex()), curCSV.getString(NFCMLContent.Geeks.Columns.TIMEWINNER.getIndex()) };
csvWrite.writeNext(arrStr);
}
Log.i(TAG, "DB succesfully exported to: " + file.getPath());
return file.getAbsolutePath();
} catch (Exception e) {
Log.e(TAG, "Error while writing the DB contents to the CSV file", e);
} finally {
// Cleanup (we don't care for any Exception thrown here)
try {
csvWrite.close();
} catch (Throwable ignored) {
}
try {
curCSV.close();
} catch (Throwable ignored) {
}
}
return null;
}Example 68
| Project: OpenEMRConnect-master File: RecordCsvWriter.java View source code |
public void writeToCsv(List<LinkedRecord> linkedRecordList, String outputDir, String fileName, String fileExtension, int recordsPerFile) throws IOException {
CSVWriter csvWriter = null;
try {
List<LinkedRecord> tempLinkedRecordList = new ArrayList<LinkedRecord>();
int i = 1;
for (LinkedRecord linkedRecord : linkedRecordList) {
tempLinkedRecordList.add(linkedRecord);
if (i % recordsPerFile == 0 || i == (linkedRecordList.size())) {
int recordCount = recordsPerFile;
if (i % recordsPerFile != 0) {
recordCount = i % recordsPerFile;
}
String filePath = createFileName(outputDir, fileName, fileExtension, recordCount);
Mediator.getLogger(RecordCsvWriter.class.getName()).log(Level.FINE, "About to write {0} record(s) to {1}", new Object[] { recordCount, filePath });
csvWriter = new CSVWriter(new FileWriter(new File(filePath)));
csvWriter.writeAll(format.format(tempLinkedRecordList));
Mediator.getLogger(RecordCsvWriter.class.getName()).log(Level.FINE, "Finished writing {0} record(s) to {1}", new Object[] { recordCount, filePath });
tempLinkedRecordList.clear();
}
i++;
}
Mediator.getLogger(RecordCsvWriter.class.getName()).log(Level.INFO, "Finished writing output");
} finally {
if (csvWriter != null) {
csvWriter.close();
}
}
}Example 69
| Project: OpenRefine-master File: CsvExporter.java View source code |
@Override
public void export(Project project, Properties params, Engine engine, final Writer writer) throws IOException {
String optionsString = (params == null) ? null : params.getProperty("options");
JSONObject options = null;
if (optionsString != null) {
try {
options = ParsingUtilities.evaluateJsonStringToObject(optionsString);
} catch (JSONException e) {
}
}
final String separator = options == null ? Character.toString(this.separator) : JSONUtilities.getString(options, "separator", Character.toString(this.separator));
final String lineSeparator = options == null ? CSVWriter.DEFAULT_LINE_END : JSONUtilities.getString(options, "lineSeparator", CSVWriter.DEFAULT_LINE_END);
final boolean quoteAll = options == null ? false : JSONUtilities.getBoolean(options, "quoteAll", false);
final boolean printColumnHeader = (params != null && params.getProperty("printColumnHeader") != null) ? Boolean.parseBoolean(params.getProperty("printColumnHeader")) : true;
final CSVWriter csvWriter = new CSVWriter(writer, separator.charAt(0), CSVWriter.DEFAULT_QUOTE_CHARACTER, lineSeparator);
TabularSerializer serializer = new TabularSerializer() {
@Override
public void startFile(JSONObject options) {
}
@Override
public void endFile() {
}
@Override
public void addRow(List<CellData> cells, boolean isHeader) {
if (!isHeader || printColumnHeader) {
String[] strings = new String[cells.size()];
for (int i = 0; i < strings.length; i++) {
CellData cellData = cells.get(i);
strings[i] = (cellData != null && cellData.text != null) ? cellData.text : "";
}
csvWriter.writeNext(strings, quoteAll);
}
}
};
CustomizableTabularExporterUtilities.exportRows(project, engine, params, serializer);
csvWriter.close();
}Example 70
| Project: ORCID-Source-master File: OrgManagerImpl.java View source code |
@Override
public void writeAmbiguousOrgs(Writer writer) {
@SuppressWarnings("resource") CSVWriter csvWriter = new CSVWriter(writer);
csvWriter.writeNext(AMBIGUOUS_ORGS_HEADER);
int firstResult = 0;
List<AmbiguousOrgEntity> chunk = null;
do {
chunk = getAmbiguousOrgs(firstResult, CHUNK_SIZE);
for (AmbiguousOrgEntity orgEntity : chunk) {
String[] line = new String[] { String.valueOf(orgEntity.getId()), orgEntity.getSourceOrcid(), orgEntity.getName(), orgEntity.getCity(), orgEntity.getRegion(), orgEntity.getCountry().value(), String.valueOf(orgEntity.getUsedCount()) };
csvWriter.writeNext(line);
}
firstResult += chunk.size();
} while (!chunk.isEmpty());
}Example 71
| Project: partake-master File: ShowParticipantsCSVAction.java View source code |
private byte[] createCSVInputStream(EventEx event, List<Pair<EventTicket, EventTicketHolderList>> ticketAndHolders, Map<String, List<String>> userTicketInfoMap) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CSVWriter writer = new CSVWriter(new OutputStreamWriter(baos, Charset.forName("UTF-8")));
writeHeader(writer, event);
for (int i = 0; i < ticketAndHolders.size(); ++i) {
EventTicket ticket = ticketAndHolders.get(i).getFirst();
EventTicketHolderList list = ticketAndHolders.get(i).getSecond();
writeTicket(writer, event, ticket, list, i, userTicketInfoMap);
}
writer.flush();
writer.close();
return baos.toByteArray();
}Example 72
| Project: ScreenSlicer-master File: Spreadsheet.java View source code |
public static String csv(List<List<String>> rows) {
StringWriter sw = new StringWriter();
CSVWriter writer = new CSVWriter(sw);
try {
List<String[]> elements = new ArrayList<String[]>();
for (List<String> row : rows) {
elements.add(row.toArray(new String[0]));
}
writer.writeAll(elements);
return sw.toString();
} finally {
try {
writer.close();
} catch (IOException e) {
Log.exception(e);
}
}
}Example 73
| Project: semantic_parser-master File: TableWriter.java View source code |
private void writeCSVActual(PrintWriter out) {
try (CSVWriter writer = new CSVWriter(out)) {
String[] record = new String[graph.numColumns()];
// Print header
for (int j = 0; j < record.length; j++) record[j] = graph.getColumn(j).originalString;
writer.writeNext(record);
// Print other rows
for (int i = 0; i < graph.numRows(); i++) {
for (int j = 0; j < record.length; j++) record[j] = graph.getCell(i, j).properties.originalString;
writer.writeNext(record);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}Example 74
| Project: sempre-master File: TableWriter.java View source code |
private void writeCSVActual(PrintWriter out) {
try (CSVWriter writer = new CSVWriter(out)) {
String[] record = new String[graph.numColumns()];
// Print header
for (int j = 0; j < record.length; j++) record[j] = graph.getColumn(j).originalString;
writer.writeNext(record);
// Print other rows
for (int i = 0; i < graph.numRows(); i++) {
for (int j = 0; j < record.length; j++) record[j] = graph.getCell(i, j).properties.originalString;
writer.writeNext(record);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}Example 75
| Project: slicer-master File: Spreadsheet.java View source code |
public static String csv(List<List<String>> rows) {
StringWriter sw = new StringWriter();
CSVWriter writer = new CSVWriter(sw);
try {
List<String[]> elements = new ArrayList<String[]>();
for (List<String> row : rows) {
elements.add(row.toArray(new String[0]));
}
writer.writeAll(elements);
return sw.toString();
} finally {
try {
writer.close();
} catch (IOException e) {
Log.exception(e);
}
}
}Example 76
| Project: sqoop-on-spark-master File: KiteToInitializer.java View source code |
@Override
public Set<String> getJars(InitializerContext context, LinkConfiguration linkConfig, ToJobConfiguration toJobConfig) {
Set<String> jars = super.getJars(context, linkConfig, toJobConfig);
jars.add(ClassUtils.jarForClass("org.kitesdk.data.Formats"));
jars.add(ClassUtils.jarForClass("com.fasterxml.jackson.databind.JsonNode"));
jars.add(ClassUtils.jarForClass("com.fasterxml.jackson.core.TreeNode"));
if (FileFormat.CSV.equals(toJobConfig.toJobConfig.fileFormat)) {
jars.add(ClassUtils.jarForClass("au.com.bytecode.opencsv.CSVWriter"));
}
if (toJobConfig.toJobConfig.uri.startsWith("dataset:hive")) {
// @TODO(Abe): Remove a deps that aren't used?
jars.add(ClassUtils.jarForClass("org.apache.hadoop.hive.conf.HiveConf"));
jars.add(ClassUtils.jarForClass("org.apache.hadoop.hive.serde2.SerDe"));
jars.add(ClassUtils.jarForClass("org.kitesdk.data.spi.hive.MetaStoreUtil"));
jars.add(ClassUtils.jarForClass("org.kitesdk.compat.DynConstructors"));
jars.add(ClassUtils.jarForClass("org.apache.hadoop.hive.metastore.Warehouse"));
jars.add(ClassUtils.jarForClass("org.apache.hive.common.HiveCompat"));
jars.add(ClassUtils.jarForClass("parquet.hive.HiveBindingFactory"));
jars.add(ClassUtils.jarForClass("com.facebook.fb303.FacebookService"));
jars.add(ClassUtils.jarForClass("org.datanucleus.query.compiler.JavaQueryCompiler"));
jars.add(ClassUtils.jarForClass("org.datanucleus.query.typesafe.TypesafeSubquery"));
jars.add(ClassUtils.jarForClass("org.datanucleus.store.rdbms.sql.SQLStatement"));
}
return jars;
}Example 77
| Project: tech-gallery-master File: ExportUtils.java View source code |
private static StringWriter createCsv(Workbook workBook) throws IOException {
Sheet sheet = workBook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
StringWriter stringWriter = new StringWriter();
CSVWriter csvOutput = new CSVWriter(stringWriter);
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
int i = 0;
String[] cellValues = new String[SHEET_CELL_SIZE];
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch(cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
cellValues[i] = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC:
cellValues[i] = String.valueOf(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_BLANK:
cellValues[i] = "";
break;
}
i = i + 1;
}
csvOutput.writeNext(cellValues);
}
csvOutput.close();
return stringWriter;
}Example 78
| Project: user-master File: OrganizationExport.java View source code |
@Override
public void runTool(CommandLine line) throws Exception {
startSpring();
setVerbose(line);
prepareBaseOutputFileName(line);
outputDir = createOutputParentDir();
String queryString = line.getOptionValue(QUERY_ARG);
Query query = Query.fromQL(queryString);
logger.info("Export directory: {}", outputDir.getAbsolutePath());
CSVWriter writer = new CSVWriter(new FileWriter(outputDir.getAbsolutePath() + "/admins.csv"), ',');
writer.writeNext(new String[] { "Org uuid", "Org Name", "Admin uuid", "Admin Name", "Admin Email", "Admin Created Date" });
Results organizations = null;
do {
organizations = getOrganizations(query);
for (Entity organization : organizations.getEntities()) {
final String orgName = organization.getProperty("path").toString();
final UUID orgId = organization.getUuid();
logger.info("Org Name: {} key: {}", orgName, orgId);
for (UserInfo user : managementService.getAdminUsersForOrganization(organization.getUuid())) {
Entity admin = managementService.getAdminUserEntityByUuid(user.getUuid());
Long createdDate = (Long) admin.getProperties().get("created");
writer.writeNext(new String[] { orgId.toString(), orgName, user.getUuid().toString(), user.getName(), user.getEmail(), createdDate == null ? "Unknown" : sdf.format(new Date(createdDate)) });
}
}
query.setCursor(organizations.getCursor());
} while (organizations != null && organizations.hasCursor());
logger.info("Completed export");
writer.flush();
writer.close();
}Example 79
| Project: usergrid-master File: OrganizationExport.java View source code |
@Override
public void runTool(CommandLine line) throws Exception {
startSpring();
setVerbose(line);
prepareBaseOutputFileName(line);
outputDir = createOutputParentDir();
String queryString = line.getOptionValue(QUERY_ARG);
Query query = Query.fromQL(queryString);
logger.info("Export directory: {}", outputDir.getAbsolutePath());
CSVWriter writer = new CSVWriter(new FileWriter(outputDir.getAbsolutePath() + "/admins.csv"), ',');
writer.writeNext(new String[] { "Org uuid", "Org Name", "Admin uuid", "Admin Name", "Admin Email", "Admin Created Date" });
Results organizations = null;
do {
organizations = getOrganizations(query);
for (Entity organization : organizations.getEntities()) {
final String orgName = organization.getProperty("path").toString();
final UUID orgId = organization.getUuid();
logger.info("Org Name: {} key: {}", orgName, orgId);
for (UserInfo user : managementService.getAdminUsersForOrganization(organization.getUuid())) {
Entity admin = managementService.getAdminUserEntityByUuid(user.getUuid());
Long createdDate = (Long) admin.getProperties().get("created");
writer.writeNext(new String[] { orgId.toString(), orgName, user.getUuid().toString(), user.getName(), user.getEmail(), createdDate == null ? "Unknown" : sdf.format(new Date(createdDate)) });
}
}
query.setCursor(organizations.getCursor());
} while (organizations != null && organizations.hasCursor());
logger.info("Completed export");
writer.flush();
writer.close();
}Example 80
| Project: vm-truck-loader-master File: CSVUtils.java View source code |
public static boolean updateLineInCsvFile(String filename, MachineSpecification spec) {
validateSpec(spec);
try {
List<String[]> entries = new CSVReader(new FileReader(filename), CSV_SEPARATOR).readAll();
CSVWriter writer = new CSVWriter(new FileWriter(filename), CSV_SEPARATOR);
for (String[] entry : entries) {
if (spec.getVmName().equals(entry[1]))
entry = turnSpecIntoCsvLine(spec, entry);
writer.writeNext(entry);
}
writer.close();
return true;
} catch (IOException e) {
throw new IllegalStateException(e);
}
}Example 81
| Project: amos-ss15-proj3-master File: MatrixTools.java View source code |
/**
* saves matrix to {@code path} in csv format
* @param matrix matrix to save
* @param path file where matrix will be saved
*/
public static boolean SaveMatrixToCsv(RequirementsTraceabilityMatrixByImpact matrix, File path) {
OutputStreamWriter writer;
try {
writer = new OutputStreamWriter(new FileOutputStream(path, true), Charset.forName("UTF-8"));
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
try (CSVWriter csvwriter = new CSVWriter(writer, ';')) {
ArrayList<String> headers = new ArrayList<>();
headers.add("File name");
java.util.List<String> reqs = matrix.getRequirements();
//create headers
for (String headerName : reqs) {
headers.add("Req-" + headerName);
}
csvwriter.writeNext(headers.toArray(new String[headers.size()]));
//add content row by row
ArrayList<String> row;
List<String> files = matrix.getFiles();
for (String fileName : files) {
row = new ArrayList<>();
row.add(fileName);
//for each req (column)
for (String req : reqs) {
RequirementFileImpactValue value = matrix.getImpactValue(new RequirementFilePair(req, fileName));
if (value == null) {
value = new RequirementFileImpactValue(0);
}
row.add(String.valueOf(value.getImpactPercentage()));
}
//write line
csvwriter.writeNext(row.toArray(new String[row.size()]));
}
} catch (IOException e) {
e.printStackTrace();
}
return true;
}Example 82
| Project: andlytics-master File: StatsCsvReaderWriter.java View source code |
@SuppressWarnings("resource")
public void writeStats(String packageName, List<AppStats> stats, ZipOutputStream zip) throws IOException {
zip.putNextEntry(new ZipEntry(packageName + CSV_SUFFIX));
// we don't own the stream, it's closed by the caller
CSVWriter writer = new CSVWriter(new OutputStreamWriter(zip));
writer.writeNext(HEADER_LIST);
String[] line = new String[HEADER_LIST.length];
for (AppStats stat : stats) {
line[0] = packageName;
line[1] = createTimestampFormat().format(stat.getDate());
line[2] = Integer.toString(stat.getTotalDownloads());
line[3] = Integer.toString(stat.getActiveInstalls());
line[4] = Integer.toString(stat.getNumberOfComments());
line[5] = Utils.safeToString(stat.getRating1());
line[6] = Utils.safeToString(stat.getRating2());
line[7] = Utils.safeToString(stat.getRating3());
line[8] = Utils.safeToString(stat.getRating4());
line[9] = Utils.safeToString(stat.getRating5());
line[10] = Utils.safeToString(stat.getVersionCode());
line[11] = Utils.safeToString(stat.getNumberOfErrors());
line[12] = stat.getTotalRevenue() == null ? "" : String.format(Locale.US, "%.2f", stat.getTotalRevenue().getAmount());
line[13] = stat.getTotalRevenue() == null ? "" : stat.getTotalRevenue().getCurrencyCode();
writer.writeNext(line);
}
writer.flush();
}Example 83
| Project: awe-core-master File: NetworkExportJob.java View source code |
/**
* @param file
* @throws IOException
*/
private void openWriterStream(final Entry<ExportedDataItems, List<SynonymsWrapper>> file) throws IOException {
String filePath = container.getDirectoryPath() + File.separator + MessageFormat.format(file.getKey().getFileNameFormat(), model.getName());
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(filePath), container.getCharset());
CSVWriter writer = new CSVWriter(osw, container.getSeparator(), container.getQuoteChar());
List<String> headers = prepareHeadersFromSynonyms(file.getKey(), file.getValue());
writer.writeNext(headers.toArray(new String[0]));
writerStreams.put(file.getKey(), writer);
}Example 84
| Project: cascading-opencsv-master File: OpenCsvScheme.java View source code |
@Override
public void sinkPrepare(final FlowProcess<JobConf> flowProcess, final SinkCall<Object[], OutputCollector> sinkCall) throws IOException {
final StringWriter stringWriter = new StringWriter(4 * 1024);
final CSVWriter csvWriter = createCsvWriter(stringWriter);
sinkCall.setContext(new Object[5]);
sinkCall.getContext()[0] = new Text();
sinkCall.getContext()[1] = stringWriter;
sinkCall.getContext()[2] = Charset.forName(charsetName);
sinkCall.getContext()[3] = csvWriter;
sinkCall.getContext()[4] = new String[getSinkFields().size()];
if (hasHeader) {
final Fields fields = sinkCall.getOutgoingEntry().getFields();
write(sinkCall, fields);
}
}Example 85
| Project: celos-master File: HiveTableDeployer.java View source code |
private Path createTempHdfsFileForInsertion(FixTable fixTable, TestRun testRun) throws Exception {
Path pathToParent = new Path(testRun.getHdfsPrefix(), ".hive");
Path pathTo = new Path(pathToParent, UUID.randomUUID().toString());
FileSystem fileSystem = testRun.getCiContext().getFileSystem();
fileSystem.mkdirs(pathTo.getParent());
FSDataOutputStream outputStream = fileSystem.create(pathTo);
CSVWriter writer = new CSVWriter(new OutputStreamWriter(outputStream), '\t', CSVWriter.NO_QUOTE_CHARACTER);
for (FixTable.FixRow fixRow : fixTable.getRows()) {
List<String> rowData = Lists.newArrayList();
for (String colName : fixTable.getColumnNames()) {
rowData.add(fixRow.getCells().get(colName));
}
String[] dataArray = rowData.toArray(new String[rowData.size()]);
writer.writeNext(dataArray);
}
writer.close();
fileSystem.setPermission(pathToParent, new FsPermission(FsAction.ALL, FsAction.ALL, FsAction.ALL));
fileSystem.setPermission(pathTo, new FsPermission(FsAction.ALL, FsAction.ALL, FsAction.ALL));
return pathTo;
}Example 86
| Project: CommandBook-master File: FlatFileLocationsManager.java View source code |
public void save() throws IOException {
FileOutputStream output = null;
try {
output = new FileOutputStream(file);
OutputStreamWriter streamWriter = new OutputStreamWriter(output, "utf-8");
BufferedWriter writer = new BufferedWriter(streamWriter);
CSVWriter csv = new CSVWriter(writer);
synchronized (this) {
Set<NamedLocation> toStore = new HashSet<NamedLocation>();
for (List<NamedLocation> locList : unloadedLocations.values()) {
toStore.addAll(locList);
}
toStore.addAll(locations.values());
for (NamedLocation warp : toStore) {
UUID ID = warp.getCreatorID();
csv.writeNext(new String[] { warp.getName(), warp.getWorldName() != null ? warp.getWorldName() : warp.getLocation().getWorld().getName(), String.valueOf(ID != null ? ID : warp.getCreatorName()), String.valueOf(warp.getLocation().getX()), String.valueOf(warp.getLocation().getY()), String.valueOf(warp.getLocation().getZ()), String.valueOf(warp.getLocation().getPitch()), String.valueOf(warp.getLocation().getYaw()) });
}
}
csv.flush();
csv.close();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException ignore) {
}
}
}
}Example 87
| Project: csv-serde-master File: CSVSerde.java View source code |
@Override
public void initialize(final Configuration conf, final Properties tbl) throws SerDeException {
final List<String> columnNames = Arrays.asList(tbl.getProperty(Constants.LIST_COLUMNS).split(","));
final List<TypeInfo> columnTypes = TypeInfoUtils.getTypeInfosFromTypeString(tbl.getProperty(Constants.LIST_COLUMN_TYPES));
numCols = columnNames.size();
final List<ObjectInspector> columnOIs = new ArrayList<ObjectInspector>(numCols);
for (int i = 0; i < numCols; i++) {
columnOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
}
this.inspector = ObjectInspectorFactory.getStandardStructObjectInspector(columnNames, columnOIs);
this.outputFields = new String[numCols];
row = new ArrayList<String>(numCols);
for (int i = 0; i < numCols; i++) {
row.add(null);
}
separatorChar = getProperty(tbl, "separatorChar", CSVWriter.DEFAULT_SEPARATOR);
quoteChar = getProperty(tbl, "quoteChar", CSVWriter.DEFAULT_QUOTE_CHARACTER);
escapeChar = getProperty(tbl, "escapeChar", CSVWriter.DEFAULT_ESCAPE_CHARACTER);
}Example 88
| Project: daxplore-presenter-master File: GetCsvServlet.java View source code |
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
PersistenceManager pm = null;
try {
// Get input from URL
String prefix = request.getParameter("prefix");
String queryString = request.getParameter("q");
String localeString = request.getParameter("l");
// Clean user input
if (prefix == null || !SharedResourceTools.isSyntacticallyValidPrefix(prefix)) {
throw new BadRequestException("Someone tried to access a syntactically invalid prefix: '" + prefix + "'");
}
if (localeString == null) {
localeString = "";
}
pm = PMF.get().getPersistenceManager();
List<String[]> csvTable = new LinkedList<>();
Locale locale = new Locale(localeString);
QuestionMetadata questionMetadata;
String key = prefix + "_" + locale.toLanguageTag();
if (metadataMap.containsKey(key)) {
questionMetadata = metadataMap.get(key);
} else {
String questionText = TextFileStore.getLocalizedFile(pm, prefix, "questions", locale, ".json");
questionMetadata = new QuestionMetadataServerImpl(new StringReader(questionText));
metadataMap.put(key, questionMetadata);
}
QueryDefinition queryDefinition = new QueryDefinition(questionMetadata, queryString);
List<String> questionOptionTexts = queryDefinition.getQuestionOptionTexts();
List<String> perspectiveOptionTexts = queryDefinition.getPerspectiveOptionTexts();
List<Integer> usedPerspectiveOptions = queryDefinition.getUsedPerspectiveOptions();
questionOptionTexts.add(0, queryDefinition.getPerspectiveShortText() + " \\ " + queryDefinition.getQuestionShortText());
//TODO handle timepoints properly
String timepoint0Text = SettingItemStore.getLocalizedProperty(pm, prefix, "usertexts", locale, "timepoint_0");
String timepoint1Text = SettingItemStore.getLocalizedProperty(pm, prefix, "usertexts", locale, "timepoint_1");
String statString = StatDataItemStore.getStats(pm, prefix, queryDefinition);
ChartDataParserServer data = new ChartDataParserServer(statString);
List<ChartDataItem> dataItems = data.getDataItems();
int columnCount = 4 + queryDefinition.getQuestionOptionCount();
int rowCount = 0;
csvTable.add(questionOptionTexts.toArray(new String[columnCount]));
if (queryDefinition.getPerspectiveOptionCount() > 0) {
for (int perspectiveOption : usedPerspectiveOptions) {
String[] row = new String[columnCount];
row[0] = MessageFormat.format("{0} ({1})", perspectiveOptionTexts.get(perspectiveOption), timepoint0Text);
int[] primaryData = dataItems.get(perspectiveOption).getCountData();
for (int i = 1; i <= queryDefinition.getQuestionOptionCount(); i++) {
row[i] = Integer.toString(primaryData[i - 1]);
}
csvTable.add(row);
rowCount++;
}
}
if (queryDefinition.hasFlag(QueryFlag.TOTAL) || queryDefinition.getPerspectiveOptionCount() == 0) {
String[] row = new String[columnCount];
String allRespondents = SettingItemStore.getLocalizedProperty(pm, prefix, "usertexts", locale, "all_respondents");
row[0] = MessageFormat.format("{0} ({1})", allRespondents, timepoint0Text);
int[] primaryData = data.getTotalDataItem().getCountData();
for (int i = 1; i <= queryDefinition.getQuestionOptionCount(); i++) {
row[i] = Integer.toString(primaryData[i - 1]);
}
csvTable.add(row);
rowCount++;
}
if (queryDefinition.hasFlag(QueryFlag.SECONDARY)) {
for (int perspectiveOption : usedPerspectiveOptions) {
String[] row = new String[columnCount];
row[0] = MessageFormat.format("{0} ({1})", perspectiveOptionTexts.get(perspectiveOption), timepoint1Text);
int[] secondaryData = dataItems.get(perspectiveOption).getCountDataSecondary();
for (int i = 1; i <= queryDefinition.getQuestionOptionCount(); i++) {
row[i] = Integer.toString(secondaryData[i - 1]);
}
csvTable.add(row);
rowCount++;
}
if (queryDefinition.hasFlag(QueryFlag.TOTAL) || queryDefinition.getPerspectiveOptionCount() == 0) {
String[] row = new String[columnCount];
String allRespondents = SettingItemStore.getLocalizedProperty(pm, prefix, "usertexts", locale, "all_respondents");
row[0] = MessageFormat.format("{0} ({1})", allRespondents, timepoint1Text);
int[] secondaryData = data.getTotalDataItem().getCountDataSecondary();
for (int i = 1; i <= queryDefinition.getQuestionOptionCount(); i++) {
row[i] = Integer.toString(secondaryData[i - 1]);
}
csvTable.add(row);
rowCount++;
}
}
while (rowCount++ < 3) {
csvTable.add(new String[columnCount]);
}
int metaDataColumn = queryDefinition.getQuestionOptionCount() + 2;
csvTable.get(0)[metaDataColumn] = SettingItemStore.getLocalizedProperty(pm, prefix, "usertexts", locale, "pageTitle");
csvTable.get(1)[metaDataColumn] = queryDefinition.getPerspectiveShortText() + ":";
csvTable.get(1)[metaDataColumn + 1] = queryDefinition.getPerspectiveFullText();
csvTable.get(2)[metaDataColumn] = queryDefinition.getQuestionShortText() + ":";
csvTable.get(2)[metaDataColumn + 1] = queryDefinition.getQuestionFullText();
// write response
try (PrintWriter respWriter = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF8"), true);
CSVWriter csvWriter = new CSVWriter(respWriter)) {
response.setContentType("text/csv; charset=UTF-8");
csvWriter.writeAll(csvTable);
csvWriter.close();
response.setStatus(HttpServletResponse.SC_OK);
} catch (IOException e) {
throw new InternalServerException("Failed to write csv response", e);
}
} catch (BadRequestException e) {
logger.log(Level.WARNING, e.getMessage(), e);
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
} catch (InternalServerException e) {
logger.log(Level.SEVERE, e.getMessage(), e);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} catch (RuntimeException e) {
logger.log(Level.SEVERE, "Unexpected exception: " + e.getMessage(), e);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} finally {
if (pm != null) {
pm.close();
}
}
}Example 89
| Project: dbpedia-spotlight-db-master File: WiktionaryExtractor.java View source code |
public static void main(String[] args) throws SAXException, IOException, RDFHandlerException, RDFParseException {
// NTriplesParser nTriplesParser = new NTriplesParser();
// nTriplesParser.parse(new FileReader(new File("/Users/jodaiber/Documents/workspace/ba/BachelorThesis/02 Implementation/DBPedia Spotlight data/confusables_with_wiktionary.nt")), "http://dbpedia.org/property/");
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
WiktionaryExtractor wiktionaryExtractor = new WiktionaryExtractor(new CSVWriter(new FileWriter(new File("/Users/jodaiber/Desktop/idioms.csv"))), new CSVWriter(new FileWriter(new File("/Users/jodaiber/Desktop/generalReference.csv"))));
List<Statement> confusables = new LinkedList<Statement>();
StatementCollector inputCollector = new StatementCollector(confusables);
NTriplesParserFactory nTriplesParserFactory = new NTriplesParserFactory();
RDFParser parser = nTriplesParserFactory.getParser();
parser.setRDFHandler(inputCollector);
parser.parse(new FileReader(new File("/Users/jodaiber/Documents/workspace/ba/BachelorThesis/01 Evaluation/03 Training/Training Set/data/confusables_with_wiktionary.nt")), "");
wiktionaryExtractor.setConfusables(confusables);
xmlReader.setContentHandler(wiktionaryExtractor);
InputSource inputSource = new InputSource(new FileReader(new File("/Users/jodaiber/Desktop/wiktionary-parser/enwiktionary-20110512-pages-articles.xml")));
xmlReader.parse(inputSource);
wiktionaryExtractor.close();
}Example 90
| Project: dlibrary-master File: CSVOutputter.java View source code |
public void setup(SourceResolver sourceResolver, Map objectModel, String src, Parameters parameters) throws IOException, SAXException, ProcessingException {
log.info("CSV Writer for stats");
super.setup(sourceResolver, objectModel, src, parameters);
try {
//super.setup(sourceResolver, objectModel, src, parameters);
this.request = ObjectModelHelper.getRequest(objectModel);
this.response = ObjectModelHelper.getResponse(objectModel);
context = ContextUtil.obtainContext(objectModel);
String requestURI = request.getRequestURI();
String[] uriSegments = requestURI.split("/");
String requestedReport = uriSegments[uriSegments.length - 1];
if (requestedReport == null || requestedReport.length() < 1) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
response.setContentType("text/csv; encoding='UTF-8'");
response.setStatus(HttpServletResponse.SC_OK);
writer = new CSVWriter(response.getWriter());
DSpaceObject dso = HandleUtil.obtainHandle(objectModel);
response.setHeader("Content-Disposition", "attachment; filename=KBStats-" + dso.getHandle() + "-" + requestedReport + ".csv");
Map<String, String> params = new HashMap<String, String>();
for (Enumeration<String> paramNames = (Enumeration<String>) request.getParameterNames(); paramNames.hasMoreElements(); ) {
String param = paramNames.nextElement();
params.put(param, request.getParameter(param));
}
String fromValue = "";
if (params.containsKey("from")) {
fromValue = params.get("from");
}
String toValue = "";
if (params.containsKey("to")) {
toValue = params.get("to");
}
Date fromDate;
ReportGenerator reportGeneratorInstance = new ReportGenerator();
if (fromValue.length() > 0) {
fromDate = reportGeneratorInstance.tryParse(fromValue);
} else {
fromDate = null;
}
Date toDate;
if (toValue.length() > 0) {
toDate = reportGeneratorInstance.tryParse(toValue);
} else {
toDate = null;
}
ElasticSearchStatsViewer esStatsViewer = new ElasticSearchStatsViewer(dso, fromDate, toDate);
StatisticsTransformer statisticsTransformerInstance = new StatisticsTransformer(fromDate, toDate);
if (requestedReport.equalsIgnoreCase("topCountries")) {
SearchRequestBuilder requestBuilder = esStatsViewer.facetedQueryBuilder(esStatsViewer.facetTopCountries);
SearchResponse searchResponse = requestBuilder.execute().actionGet();
TermsFacet topCountriesFacet = searchResponse.getFacets().facet(TermsFacet.class, "top_countries");
addTermFacetToWriter(topCountriesFacet, "");
} else if (requestedReport.equalsIgnoreCase("fileDownloads")) {
SearchRequestBuilder requestBuilder = esStatsViewer.facetedQueryBuilder(esStatsViewer.facetMonthlyDownloads);
SearchResponse searchResponse = requestBuilder.execute().actionGet();
DateHistogramFacet monthlyDownloadsFacet = searchResponse.getFacets().facet(DateHistogramFacet.class, "monthly_downloads");
addDateHistogramFacetToWriter(monthlyDownloadsFacet);
} else if (requestedReport.equalsIgnoreCase("topDownloads")) {
SearchRequestBuilder requestBuilder = esStatsViewer.facetedQueryBuilder(esStatsViewer.facetTopBitstreamsAllTime);
SearchResponse searchResponse = requestBuilder.execute().actionGet();
log.info(searchResponse.toString());
TermsFacet topBitstreams = searchResponse.getFacets().facet(TermsFacet.class, "top_bitstreams_alltime");
addTermFacetToWriter(topBitstreams, "bitstream");
} else {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}
} catch (SQLException e) {
log.error("Some Error:" + e.getMessage());
} catch (WingException e) {
log.error("Some Error:" + e.getMessage());
} catch (IOException e) {
log.error("Some Error:" + e.getMessage());
} finally {
try {
if (writer != null) {
writer.close();
} else {
log.error("CSV Writer was null!!");
}
} catch (IOException e) {
log.error("Hilarity Ensues... IO Exception while closing the csv writer.");
}
}
}Example 91
| Project: DSpace-master File: Dataset.java View source code |
public ByteArrayOutputStream exportAsCSV() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CSVWriter ecsvp = new CSVWriter(new OutputStreamWriter(baos), ';');
//Generate the item row
List<String> colLabels = getColLabels();
colLabels.add(0, "");
ecsvp.writeNext(colLabels.toArray(new String[colLabels.size()]));
List<String> rowLabels = getRowLabels();
String[][] matrix = getMatrix();
for (int i = 0; i < rowLabels.size(); i++) {
String rowLabel = rowLabels.get(i);
ecsvp.writeNext((String[]) ArrayUtils.addAll(new String[] { rowLabel }, matrix[i]));
}
ecsvp.flush();
ecsvp.close();
return baos;
}Example 92
| Project: gradebookNG-master File: GradeImportUploadStep.java View source code |
private File buildFile(boolean includeGrades) {
File tempFile;
try {
//TODO - add the site name to the file?
tempFile = File.createTempFile("gradebookTemplate", ".csv");
FileWriter fw = new FileWriter(tempFile);
CSVWriter csvWriter = new CSVWriter(fw);
//Create csv header
List<String> header = new ArrayList<String>();
header.add("Student ID");
header.add("Student Name");
for (Assignment assignment : assignments) {
String assignmentPoints = assignment.getPoints().toString();
header.add(assignment.getName() + " [" + StringUtils.removeEnd(assignmentPoints, ".0") + "]");
header.add("*/ " + assignment.getName() + " Comments */");
}
csvWriter.writeNext(header.toArray(new String[] {}));
for (GbStudentGradeInfo studentGradeInfo : grades) {
List<String> line = new ArrayList<String>();
line.add(studentGradeInfo.getStudentEid());
line.add(studentGradeInfo.getStudentLastName() + ", " + studentGradeInfo.getStudentFirstName());
if (includeGrades) {
for (Assignment assignment : assignments) {
GbGradeInfo gradeInfo = studentGradeInfo.getGrades().get(assignment.getId());
if (gradeInfo != null) {
line.add(StringUtils.removeEnd(gradeInfo.getGrade(), ".0"));
line.add(gradeInfo.getGradeComment());
} else {
// Need to account for no grades
line.add(null);
line.add(null);
}
}
}
csvWriter.writeNext(line.toArray(new String[] {}));
}
csvWriter.close();
fw.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return tempFile;
}Example 93
| Project: hive-master File: OpenCSVSerde.java View source code |
@Override
public void initialize(final Configuration conf, final Properties tbl) throws SerDeException {
final List<String> columnNames = Arrays.asList(tbl.getProperty(serdeConstants.LIST_COLUMNS).split(","));
numCols = columnNames.size();
final List<ObjectInspector> columnOIs = new ArrayList<ObjectInspector>(numCols);
for (int i = 0; i < numCols; i++) {
columnOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
}
inspector = ObjectInspectorFactory.getStandardStructObjectInspector(columnNames, columnOIs);
outputFields = new String[numCols];
row = new ArrayList<String>(numCols);
for (int i = 0; i < numCols; i++) {
row.add(null);
}
separatorChar = getProperty(tbl, SEPARATORCHAR, CSVWriter.DEFAULT_SEPARATOR);
quoteChar = getProperty(tbl, QUOTECHAR, CSVWriter.DEFAULT_QUOTE_CHARACTER);
escapeChar = getProperty(tbl, ESCAPECHAR, CSVWriter.DEFAULT_ESCAPE_CHARACTER);
}Example 94
| Project: jogetworkflow-master File: SetupWebController.java View source code |
@RequestMapping(value = "/settings/setup/directoryManagerImpl/config/submit", method = RequestMethod.POST)
public String setupDirectoryManagerImplConfigSubmit(ModelMap map, @RequestParam("directoryManagerImpl") String directoryManagerImpl, HttpServletRequest request) {
//request params
Map<String, String> propertyMap = new HashMap();
Enumeration e = request.getParameterNames();
while (e.hasMoreElements()) {
String paramName = (String) e.nextElement();
//ignore the parameter "directoryManagerImpl"
if (!paramName.equals("directoryManagerImpl")) {
String[] paramValue = (String[]) request.getParameterValues(paramName);
propertyMap.put(paramName, CsvUtil.getDeliminatedString(paramValue));
}
}
StringWriter sw = new StringWriter();
try {
CSVWriter writer = new CSVWriter(sw);
Iterator it = propertyMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> pairs = (Map.Entry) it.next();
writer.writeNext(new String[] { pairs.getKey(), pairs.getValue() });
}
writer.close();
} catch (Exception ex) {
LogUtil.error(getClass().getName(), ex, "");
}
//save plugin
Setting setting = setupManager.getSettingByProperty("directoryManagerImpl");
if (setting == null) {
setting = new Setting();
setting.setProperty("directoryManagerImpl");
}
setting.setValue(directoryManagerImpl);
setupManager.saveSetting(setting);
//save plugin properties
Setting propertySetting = setupManager.getSettingByProperty("directoryManagerImplProperties");
if (propertySetting == null) {
propertySetting = new Setting();
propertySetting.setProperty("directoryManagerImplProperties");
}
propertySetting.setValue(sw.toString());
setupManager.saveSetting(propertySetting);
return "setup/directoryManagerImplConfigSuccess";
}Example 95
| Project: knime-scripting-master File: PythonTableConverter.java View source code |
/**
* Write a table to a CSV file. The first line of the file is a CSV array of column names, the second is
* a CSV array of column types, all subsequent lines are CSV rows of the table.
*
* @param exec
* @param inputTable
* @param kInFile
* @throws RuntimeException
*/
public static void convertTableToCSV(ExecutionContext exec, BufferedDataTable inputTable, File kInFile, NodeLogger logger) throws RuntimeException {
try {
CSVWriter writer = new CSVWriter(new BufferedWriter(new FileWriter(kInFile)), ',', '\"');
DataTableSpec tableSpec = inputTable.getDataTableSpec();
List<String> columnNames = getColumnNames(tableSpec);
// The first line is the column names
writer.writeNext(columnNames.toArray(new String[0]));
// Build a list of column types
List<DataType> colTypes = getColumnTypes(tableSpec);
ArrayList<String> outputTypes = new ArrayList<String>();
for (DataType type : colTypes) {
if (type.equals(IntCell.TYPE))
outputTypes.add("INT");
else if (type.equals(DoubleCell.TYPE))
outputTypes.add("FLOAT");
else if (type.equals(StringCell.TYPE))
outputTypes.add("STRING");
else if (type.isCompatible(StringValue.class))
outputTypes.add("STRING");
}
// The second line is the column types
writer.writeNext(outputTypes.toArray(new String[0]));
// Add all the table values, row by row
ArrayList<String> rowValues = new ArrayList<String>();
for (DataRow dataRow : inputTable) {
int colNum = 0;
for (DataColumnSpec columnSpec : tableSpec) {
DataType colType = colTypes.get(colNum);
DataCell cell = dataRow.getCell(colNum++);
InputTableAttribute a = new InputTableAttribute(columnSpec.getName(), inputTable);
//Attribute a = new Attribute(columnSpec.getName(), columnSpec.getType());
if (cell.isMissing())
rowValues.add("");
else if (colType.equals(StringCell.TYPE)) {
rowValues.add(a.getNominalAttribute(dataRow));
} else if (colType.equals(DoubleCell.TYPE)) {
rowValues.add(a.getDoubleAttribute(dataRow).toString());
} else if (colType.equals(IntCell.TYPE)) {
rowValues.add(a.getIntAttribute(dataRow).toString());
} else {
rowValues.add(a.getRawValue(dataRow));
}
}
// All the columns have been added so write it out
writer.writeNext(rowValues.toArray(new String[0]));
rowValues.clear();
}
writer.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}Example 96
| Project: logbook-master File: ShipParameterRecord.java View source code |
public static void store() throws IOException {
// 変更ã?Œã?‚ã?£ã?Ÿã?¨ã??ã? ã?‘書ã??込む
if (modified) {
try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(AppConstants.SHIP_PARAMETER_FILE)), AppConstants.CHARSET), CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER)) {
List<String> flatten = new ArrayList<String>();
writer.writeNext(getHeader());
for (Entry<Integer, ShipParameterRecord> e : SHIP.entrySet()) {
ShipParameterRecord data = e.getValue();
flatten.add(String.valueOf(data.shipId));
flatten.add(data.shipName);
addToString(flatten, data.ASW);
addToString(flatten, data.Evasion);
addToString(flatten, data.LOS);
if (data.defaultSlot != null) {
addToString(flatten, data.defaultSlot);
} else {
flatten.addAll(Arrays.asList(new String[] { "null", "null", "null", "null", "null" }));
}
flatten.add(data.albumMessage);
if (data.maxeq != null) {
addToString(flatten, data.maxeq);
} else {
flatten.addAll(Arrays.asList(new String[] { "null", "null", "null", "null", "null" }));
}
flatten.add(data.seiku != null ? String.valueOf(data.seiku) : "null");
writer.writeNext(flatten.toArray(new String[0]));
flatten.clear();
}
}
ApplicationMain.sysPrint("艦パラメータファイル更新");
modified = false;
}
}Example 97
| Project: magma-master File: CsvValueTableWriter.java View source code |
private void writeVariableToCsv(String... strings) throws IOException {
try (CSVWriter writer = valueTable.getVariableWriter()) {
if (writer == null) {
throw new DatasourceParsingException("Cannot create variable writer. Table " + valueTable.getName() + " does not have variable file.", "CsvCannotCreateWriter", valueTable.getName());
}
log.trace("write '{}'", Arrays.toString(strings));
writer.writeNext(strings);
}
}Example 98
| Project: matrix-toolkits-java-master File: LinkedSparseMatrixTest.java View source code |
/**
* Does a naive perf test against DenseMatrix, outputting CSV that we plot
* in R.
* <p/>
* Generate n x n matrix with m entries, on left, and an n x n dense matrix
* with m entries on the right, using the same population algo. Then we
* multiply them and output into a dense matrix. We compare dense vs linked
* sparse from the left, and also look at memory usage. We repeat for 10
* different values of m (10,000 to 100,000), and in both cases vary m from
* 1,000 to 10,000. This requires about 8GB heap to be on the safe side.
* <p/>
* -Xms8g -Xmx8g -Djava.util.logging.config.file=logging.properties
*
* @param args
*/
public static void main(String[] args) throws Exception {
File file = new File("LinkedSparseMatrixPerf.csv");
log.info("writing to " + file);
@Cleanup CSVWriter csv = new CSVWriter(new FileWriter(file));
for (int r = 0; r < 10; r++) {
for (int m = 10000; m <= 100000; m = m + 10000) {
for (int n = 1000; n <= 10000; n = n + 1000) {
int[][] patternA = Utilities.getRowPattern(n, n, m / n);
DenseMatrix origA = new DenseMatrix(n, n);
Utilities.rowPopulate(origA, patternA);
int[][] patternB = Utilities.getRowPattern(n, n, m / n);
DenseMatrix origB = new DenseMatrix(n, n);
Utilities.rowPopulate(origB, patternB);
// to be fair, we reuse the same matrix values
long denseMem, denseInitTime, denseMultTime, sparseMem, sparseInitTime, sparseMultTime;
Stopwatch timer = Stopwatch.createUnstarted();
{
timer.reset();
timer.start();
DenseMatrix A = new DenseMatrix(origA);
timer.stop();
// all attempts to measure memory usage failed
denseMem = n * n * 8;
denseInitTime = timer.elapsed(TimeUnit.NANOSECONDS);
timer.reset();
DenseMatrix B = origB.copy();
DenseMatrix C = new DenseMatrix(n, n);
timer.start();
A.mult(B, C);
timer.stop();
denseMultTime = timer.elapsed(TimeUnit.NANOSECONDS);
}
{
timer.reset();
timer.start();
LinkedSparseMatrix A = new LinkedSparseMatrix(origA);
timer.stop();
// using compressedooms
sparseMem = m * 28 + 16 * n;
sparseInitTime = timer.elapsed(TimeUnit.NANOSECONDS);
timer.reset();
DenseMatrix B = origB.copy();
DenseMatrix C = new DenseMatrix(n, n);
timer.start();
A.mult(B, C);
timer.stop();
sparseMultTime = timer.elapsed(TimeUnit.NANOSECONDS);
}
String[] line = new String[] { Integer.toString(n), Integer.toString(m), Long.toString(denseMem), Long.toString(denseInitTime), Long.toString(denseMultTime), Long.toString(sparseMem), Long.toString(sparseInitTime), Long.toString(sparseMultTime) };
log.info(java.util.Arrays.toString(line));
csv.writeNext(line);
// these are to keep lots of refs above alive from GC
log.finest(origA.numRows() + " " + origB.numColumns() + " " + patternA.length + " " + patternB.length);
}
}
}
}Example 99
| Project: neo4j-shell-tools-master File: ImportCypherApp.java View source code |
@Override
public Continuation execute(AppCommandParser parser, Session session, Output out) throws Exception {
Config config = Config.fromOptions(parser);
char delim = delim(parser.option("d", ","));
int batchSize = Integer.parseInt(parser.option("b", String.valueOf(Config.DEFAULT_BATCH_SIZE)));
boolean quotes = parser.options().containsKey("q");
String inputFileName = parser.option("i", null);
boolean silent = parser.options().containsKey("s");
CountingReader inputFile = FileUtils.readerFor(inputFileName);
String fileName = parser.option("o", null);
Writer outputFile = FileUtils.getPrintWriter(fileName, out);
String query = Config.extractQuery(parser);
if (!silent) {
out.println(String.format("Query: %s infile %s delim '%s' quoted %s outfile %s batch-size %d", query, name(inputFileName), delim, quotes, name(fileName), batchSize));
}
CSVReader reader = createReader(inputFile, config);
CSVWriter writer = createWriter(outputFile, config);
int count;
if (reader == null) {
count = execute(query, writer);
} else {
count = executeOnInput(reader, query, writer, config, new ProgressReporter(inputFile, out));
}
if (!silent) {
out.println("Import statement execution created " + count + " rows of output.");
}
if (reader != null)
reader.close();
if (writer != null)
writer.close();
return Continuation.INPUT_COMPLETE;
}Example 100
| Project: nerdle-master File: CSVExporter.java View source code |
public static void main(String[] args) throws Exception {
if (args.length != 3) {
System.err.println("usage: input_dir csv_dir topic.");
System.err.println("input_dir: directory of the input json files.");
System.err.println("csv_dir: directory of the the csv files.");
System.err.println("topic: name the topic.");
return;
}
File inputDir = new File(args[0]);
if (!inputDir.isDirectory()) {
System.err.println(inputDir);
System.err.println("Directory does not exist.");
return;
}
File csvDir = new File(args[1]);
String topicName = args[2];
File topicDir = new File(csvDir.getPath() + File.separator + topicName + File.separator);
if (topicDir.exists()) {
System.err.println(inputDir);
System.err.println("Topic already exists.");
return;
} else {
topicDir.mkdirs();
}
File[] files = inputDir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().startsWith("part-");
}
});
CSVWriter factswriter = new CSVWriter(new FileWriter(topicDir.getPath() + File.separator + "facts.csv"), '\t', CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER);
CSVWriter argumentswriter = new CSVWriter(new FileWriter(topicDir.getPath() + File.separator + "arguments.csv"), '\t', CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER);
Gson gson = new Gson();
BufferedReader reader;
int index = 0;
int factIndex = 1;
int argumentIndex = 1;
for (File file : files) {
System.out.println(file.getName());
reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
if ((index % 1000000) == 0) {
System.out.println();
}
if ((index % 100000) == 0) {
System.out.print(".");
}
String[] split = line.split("\t");
NerdleFact fact = gson.fromJson(split[0], NerdleFact.class);
fact = cleanFact(fact);
if (fact.getArguments().size() >= 2) {
boolean found = false;
for (NerdleArg nerdleArg : fact.getArguments()) {
if (nerdleArg.getArgLabel().matches("A[0-9]")) {
found = true;
break;
}
}
if (found && fact.getSentence().length() < MAX_SENTENCE_LENGTH) {
writeEntriesCSV(factswriter, factIndex, argumentswriter, argumentIndex, fact);
factIndex++;
argumentIndex += fact.getArguments().size();
}
}
index++;
}
}
factswriter.close();
argumentswriter.close();
}Example 101
| Project: opennms_dashboard-master File: ExportAssetsServlet.java View source code |
/** {@inheritDoc} */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Asset[] assets = null;
try {
assets = this.model.getAllAssets();
} catch (SQLException e) {
throw new ServletException("Database exception", e);
}
response.setContentType("text/plain");
CSVWriter out = new CSVWriter(response.getWriter());
String[] header = { "Node Label", "Node ID", "Category", "Manufacturer", "Vendor", "Model Number", "Serial Number", "Description", "Circuit ID", "Asset Number", "Operating System", "Rack", "Slot", "Port", "Region", "Division", "Department", "Address 1", "Address 2", "City", "State", "Zip", "Building", "Floor", "Room", "Vendor Phone", "Vendor Fax", "Date Installed", "Lease", "Lease Expires", "Support Phone", "Maint Contract", "Vendor Asset Number", "Maint Contract Expires", "Display Category", "Notification Category", "Poller Category", "Threshold Category", "Username", "Password", "Enable", "Connection", "Auto Enable", "Comments", "Cpu", "Ram", "Storage Controller", "HDD 1", "HDD 2", "HDD 3", "HDD 4", "HDD 5", "HDD 6", "Number of power supplies", "Inputpower", "Additional hardware", "Admin", "SNMP Community", "Rack unit height" };
out.writeNext(header);
// print a single line for each asset
for (int i = 0; i < assets.length; i++) {
Asset asset = assets[i];
ArrayList<String> entries = new ArrayList<String>();
entries.add(NetworkElementFactory.getInstance(getServletContext()).getNodeLabel(asset.getNodeId()));
entries.add(Integer.toString(asset.getNodeId()));
entries.add(asset.getCategory());
entries.add(asset.getManufacturer());
entries.add(asset.getVendor());
entries.add(asset.getModelNumber());
entries.add(asset.getSerialNumber());
entries.add(asset.getDescription());
entries.add(asset.getCircuitId());
entries.add(asset.getAssetNumber());
entries.add(asset.getOperatingSystem());
entries.add(asset.getRack());
entries.add(asset.getSlot());
entries.add(asset.getPort());
entries.add(asset.getRegion());
entries.add(asset.getDivision());
entries.add(asset.getDepartment());
entries.add(asset.getAddress1());
entries.add(asset.getAddress2());
entries.add(asset.getCity());
entries.add(asset.getState());
entries.add(asset.getZip());
entries.add(asset.getBuilding());
entries.add(asset.getFloor());
entries.add(asset.getRoom());
entries.add(asset.getVendorPhone());
entries.add(asset.getVendorFax());
entries.add(asset.getDateInstalled());
entries.add(asset.getLease());
entries.add(asset.getLeaseExpires());
entries.add(asset.getSupportPhone());
entries.add(asset.getMaintContract());
entries.add(asset.getVendorAssetNumber());
entries.add(asset.getMaintContractExpires());
entries.add(asset.getDisplayCategory());
entries.add(asset.getNotifyCategory());
entries.add(asset.getPollerCategory());
entries.add(asset.getThresholdCategory());
entries.add(asset.getUsername());
entries.add(asset.getPassword());
entries.add(asset.getEnable());
entries.add(asset.getConnection());
entries.add(asset.getAutoenable());
entries.add(asset.getComments());
entries.add(asset.getCpu());
entries.add(asset.getRam());
entries.add(asset.getStoragectrl());
entries.add(asset.getHdd1());
entries.add(asset.getHdd2());
entries.add(asset.getHdd3());
entries.add(asset.getHdd4());
entries.add(asset.getHdd5());
entries.add(asset.getHdd6());
entries.add(asset.getNumpowersupplies());
entries.add(asset.getInputpower());
entries.add(asset.getAdditionalhardware());
entries.add(asset.getAdmin());
entries.add(asset.getSnmpcommunity());
entries.add(asset.getRackunitheight());
out.writeNext(entries.toArray(new String[0]));
}
out.close();
}