Package au.com.bytecode.opencsv

Examples of au.com.bytecode.opencsv.CSVWriter


     */
    public static void outputResults( OutputStream os, String expId, DATA_FORMAT format ) throws IOException {
        log.info("Writing out experiment "+expId+" as "+format);

        Writer out = new BufferedWriter( new OutputStreamWriter( os, "UTF-8" ) );
        CSVWriter writer = new CSVWriter(out);

        long id = Long.parseLong(expId);

        ExperimentPersistencyRemote edao = ExperimentPersistencyImpl.getInstance();
        Experiment exp = edao.findExperiment(id);

        // The string array
        String sa[] = new String[8];
        sa[0] = "Name";
        sa[1] = "Run #";
        sa[2] = "Date";
        sa[3] = "Digital Object #";
        sa[4] = "Digital Object Source";
        sa[5] = "Stage";
        sa[6] = "Property Identifier";
        sa[7] = "Property Value";
        // write the headers out:
        writer.writeNext(sa);
       
        // Loop through:
        int bi = 1;
        for( BatchExecutionRecordImpl batch : exp.getExperimentExecutable().getBatchExecutionRecords() ) {
            // log.info("Found batch... "+batch);
            int doi = 1;
            for( ExecutionRecordImpl exr : batch.getRuns() ) {
                // log.info("Found Record... "+exr+" stages: "+exr.getStages());
                if( exr != null && exr.getStages() != null ) {
                    for( ExecutionStageRecordImpl exsr : exr.getStages() ) {
                        // log.info("Found Stage... "+exsr);
                        for( MeasurementImpl m : exsr.getMeasurements() ) {
                            // log.info("Looking at result for property "+m.getIdentifier());
                            sa[0] = exp.getExperimentSetup().getBasicProperties().getExperimentName();
                            sa[1] = "" + bi;
                            sa[2] = batch.getStartDate().getTime().toString();
                            sa[3] = "" + doi;
                            sa[4] = exr.getDigitalObjectSource();
                            sa[5] = exsr.getStage();
                            sa[6] = m.getIdentifier();
                            sa[7] = m.getValue();
                            // Write out CSV:
                            writer.writeNext( sa );
                        }
                    }
                }
                // Increment, for the next DO.
                doi++;
View Full Code Here


   
    public static void outputAnalysis( OutputStream os, String expId, DATA_FORMAT format ) throws IOException {
        log.info("Writing out experiment "+expId+" as "+format);

        Writer out = new BufferedWriter( new OutputStreamWriter( os, "UTF-8" ) );
        CSVWriter writer = new CSVWriter(out);

        long id = Long.parseLong(expId);

        ExperimentPersistencyRemote edao = ExperimentPersistencyImpl.getInstance();
        Experiment exp = edao.findExperiment(id);

        // The string array
        String sa[] = new String[8];
        sa[0] = "Name";
        sa[1] = "Digital Object #";
        sa[2] = "Digital Object Source";
        sa[3] = "Agent Type";
        sa[4] = "Agent Name";
        sa[5] = "User Environment Description";
        sa[6] = "Property Identifier";
        sa[7] = "Property Value";
        // write the headers out:
        writer.writeNext(sa);
       
        // Loop through:
        int bi = 1;
        for( BatchExecutionRecordImpl batch : exp.getExperimentExecutable().getBatchExecutionRecords() ) {
            // log.info("Found batch... "+batch);
            int doi = 1;
            for( ExecutionRecordImpl exr : batch.getRuns() ) {
                // log.info("Found Record... "+exr+" stages: "+exr.getStages());
                if( exr != null ) {
                    for( MeasurementEventImpl me : exr.getMeasurementEvents() ) {
                        for( MeasurementImpl m : me.getMeasurements() ) {
                            sa[0] = exp.getExperimentSetup().getBasicProperties().getExperimentName();
                            sa[1] = "" + doi;
                            sa[2] = exr.getDigitalObjectReferenceCopy();
                            sa[3] = me.getAgent().getType().toString();
                            sa[4] = me.getAgent().getName();
                            sa[5] = me.getAgent().getUserEnvironmentDescription();
                            sa[6] = m.getIdentifier();
                            sa[7] = m.getValue();
                            // Write out CSV:
                            writer.writeNext( sa );
                        }
                    }
                }
                // Increment, for the next DO.
                doi++;
View Full Code Here

    // create a writer
    try {
      File tmpFile = File.createTempFile("sessionData", "csv");
      FileOutputStream fos = new FileOutputStream(tmpFile, false);
      CSVWriter writer = new CSVWriter(new OutputStreamWriter(fos,
          "ISO-8859-1"), ';');

      // write the csv file header
      List<String> headers = new ArrayList<String>(baseProperties);
      headers.addAll(sessionProperties);
      writer.writeNext(headers.toArray(new String[headers.size()]));

      // fetch each session and trial, write one line for each trial
      List<ModuleSession> sessions = dataService.getSessions(module);
      for (ModuleSession session : sessions) {
        // skip if we only export some of the session
        if (fromSessionIndex >= 0
            && session.getIndex() < fromSessionIndex) {
          continue;
        }
        List<String[]> sessionData = getDataForSession(session);
        writer.writeAll(sessionData);
      }

      // close the writer and return the file
      writer.close();
      return tmpFile;
    } catch (IOException ioe) {
      logger.error("Unable to write csv file", ioe);
      return null;
    }
View Full Code Here

    public CsvPrinter(List<String> fieldNames, Writer writer, boolean header)
    {
        checkNotNull(fieldNames, "fieldNames is null");
        checkNotNull(writer, "writer is null");
        this.fieldNames = ImmutableList.copyOf(fieldNames);
        this.writer = new CSVWriter(writer);
        this.needHeader = header;
    }
View Full Code Here

        // create a writer
        try {
            File tmpFile = File.createTempFile("trialData", "csv");
            FileOutputStream fos = new FileOutputStream(tmpFile, false);
            CSVWriter writer = new CSVWriter(new OutputStreamWriter(fos,"ISO-8859-1"), separator);

            // write the csv file header
            List<String> headers = new ArrayList<String>(baseProperties);
           
            // try to use shorthand headers if unique
            HashSet<String> trialPropertiesShort = new HashSet<String>();
            for (String name : trialProperties) {
              String newName = "trial.";
              String[] nameParts = name.split("\\.");
              if (nameParts.length > 1) {
                newName += nameParts[nameParts.length - 2] + ".";
              }
              newName += nameParts[nameParts.length - 1];
              trialPropertiesShort.add(newName);
            }

            if (trialPropertiesShort.size() == trialProperties.size()) {
              useTrialShortNames = true;
              trialProperties.clear();
              trialProperties.addAll(trialPropertiesShort);
              Collections.sort(trialProperties, String.CASE_INSENSITIVE_ORDER);
              headers.addAll(trialProperties);
            } else {
              Collections.sort(trialProperties, String.CASE_INSENSITIVE_ORDER);
              headers.addAll(trialProperties);
            }

            writer.writeNext(headers.toArray(new String[headers.size()]));

            // fetch each session and trial, write one line for each trial
            List<ModuleSession> sessions = dataService.getSessions(module);
            for (ModuleSession session : sessions) {
                // skip if we only export some of the session
                if (fromSessionIndex >= 0 && session.getIndex() < fromSessionIndex) {
                    continue;
                }
               
                // export all trials for this session
                List<Trial> trials = dataService.getTrials(session);
                for (Trial trial : trials) {
                    List<String[]> trialData = getDataForTrial(session, trial);
                    writer.writeAll(trialData);
                }
            }
           
            // close the writer and return the file
            writer.close();
            return tmpFile;
        } catch (IOException ioe) {
            logger.error("Unable to write csv file", ioe);
            return null;
        }
View Full Code Here

        // create a writer
        try {
            File tmpFile = File.createTempFile("moduleData", "csv");
            FileOutputStream fos = new FileOutputStream(tmpFile, false);
            CSVWriter writer = new CSVWriter(new OutputStreamWriter(fos,"ISO-8859-1"), ';');
 
            // write the csv file header
            List<String> headers = new ArrayList<String>(baseProperties);
            headers.addAll(moduleProperties);
            writer.writeNext(headers.toArray(new String[headers.size()]));
  
            // fetch module data
            List<String[]> moduleData = getDataForModule(module);
            writer.writeAll(moduleData);
           
            // close the writer and return the file
            writer.close();
            return tmpFile;
        } catch (IOException ioe) {
            logger.error("Unable to write csv file", ioe);
            return null;
        }
View Full Code Here

  public File exportData(Module module) {
    // create a writer
        try {
            File tmpFile = File.createTempFile("accountData", "csv");
            FileOutputStream fos = new FileOutputStream(tmpFile, false);
            CSVWriter writer = new CSVWriter(new OutputStreamWriter(fos,"ISO-8859-1"), ';');
 
            // write the csv file header
            List<String> headers = new ArrayList<String>(baseProperties);
            headers.addAll(accountProperties);
            writer.writeNext(headers.toArray(new String[headers.size()]));
  
            // fetch account data
            List<String[]> moduleData = getDataForAccount(module);
            writer.writeAll(moduleData);
           
            // close the writer and return the file
            writer.close();
            return tmpFile;
        } catch (IOException ioe) {
            logger.error("Unable to write csv file", ioe);
            return null;
        }
View Full Code Here

    // create a writer
    try {
      File tmpFile = File.createTempFile("sessionData", "csv");
      FileOutputStream fos = new FileOutputStream(tmpFile, false);
      CSVWriter writer = new CSVWriter(new OutputStreamWriter(fos,
          "ISO-8859-1"), ';');

      // write the csv file header
      List<String> headers = new ArrayList<String>(baseProperties);
      headers.addAll(sessionProperties);
      writer.writeNext(headers.toArray(new String[headers.size()]));

      // fetch each session and trial, write one line for each trial
      List<ModuleSession> sessions = dataService.getSessions(module);
      for (ModuleSession session : sessions) {
        // skip if we only export some of the session
        if (fromSessionIndex >= 0
            && session.getIndex() < fromSessionIndex) {
          continue;
        }
        List<String[]> sessionData = getDataForSession(session);
        writer.writeAll(sessionData);
      }

      // close the writer and return the file
      writer.close();
      return tmpFile;
    } catch (IOException ioe) {
      logger.error("Unable to write csv file", ioe);
      return null;
    }
View Full Code Here

  public static void convert( String inputFilename, String outputFilename ) throws IOException
  {
    FileReader fr = new FileReader( inputFilename );
    CSVReader reader = new CSVReader( fr );
    FileWriter fw = new FileWriter( outputFilename );
    CSVWriter writer = new CSVWriter( fw );

    // Skip changing the first line
    String[] line = reader.readNext();
    writer.writeNext( line );

    while( ( line = reader.readNext() ) != null )
    {
      for( int i = 0; i < line.length; i++ )
      {
        line[i] = "a" + line[i];
      }
      writer.writeNext( line );
    }

    reader.close();
    fr.close();
    writer.close();
    fw.close();
  }
View Full Code Here

        // create a writer
        try {
            File tmpFile = File.createTempFile("trialData", "csv");
            FileOutputStream fos = new FileOutputStream(tmpFile, false);
            CSVWriter writer = new CSVWriter(new OutputStreamWriter(fos,"ISO-8859-1"), separator);

            // write the csv file header
            List<String> headers = new ArrayList<String>(baseProperties);
           
            // try to use shorthand headers if unique
            HashSet<String> trialPropertiesShort = new HashSet<String>();
            for (String name : trialProperties) {
              String newName = "trial.";
              String[] nameParts = name.split("\\.");
              if (nameParts.length > 1) {
                newName += nameParts[nameParts.length - 2] + ".";
              }
              newName += nameParts[nameParts.length - 1];
              trialPropertiesShort.add(newName);
            }

            if (trialPropertiesShort.size() == trialProperties.size()) {
              useTrialShortNames = true;
              trialProperties.clear();
              trialProperties.addAll(trialPropertiesShort);
              Collections.sort(trialProperties, String.CASE_INSENSITIVE_ORDER);
              headers.addAll(trialProperties);
            } else {
              Collections.sort(trialProperties, String.CASE_INSENSITIVE_ORDER);
              headers.addAll(trialProperties);
            }

            writer.writeNext(headers.toArray(new String[headers.size()]));

            // fetch each session and trial, write one line for each trial
            List<ModuleSession> sessions = dataService.getSessions(module);
            for (ModuleSession session : sessions) {
                // skip if we only export some of the session
                if (fromSessionIndex >= 0 && session.getIndex() < fromSessionIndex) {
                    continue;
                }
               
                // export all trials for this session
                List<Trial> trials = dataService.getTrials(session);
                for (Trial trial : trials) {
                    List<String[]> trialData = getDataForTrial(session, trial);
                    writer.writeAll(trialData);
                }
            }
           
            // close the writer and return the file
            writer.close();
            return tmpFile;
        } catch (IOException ioe) {
            logger.error("Unable to write csv file", ioe);
            return null;
        }
View Full Code Here

TOP

Related Classes of au.com.bytecode.opencsv.CSVWriter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.