public static void main(String[] args)
      throws InterruptedException, AlreadyConnectedException, JIException, IOException,
          NotConnectedException, DuplicateGroupException, AddFailedException {

    final FileWriter fileWriter = new FileWriter("data.csv");
    fileWriter.write("sep=,\n");
    fileWriter.flush();
    CSVWriter writer = new CSVWriter(fileWriter, ',', '\0');
    writer.writeNext(
        Tag.TAG_TO_ID_MAPPING
            .entrySet()
            .stream()
            .map(tagStringEntry -> tagStringEntry.getKey().toString())
            .collect(Collectors.toCollection(LinkedList::new))
            .toArray(new String[0]));

    final OPCDataReader opcDataReader = new OPCDataReader(Tag.TAG_TO_ID_MAPPING).startReading();

    Thread.sleep(2000);

    while (true) {
      final Map<Tag, Double> actualValues = opcDataReader.getActualValues();
      System.out.println(actualValues);
      final String[] data =
          Tag.TAG_TO_ID_MAPPING
              .entrySet()
              .stream()
              .map(tagStringEntry -> "" + actualValues.get(tagStringEntry.getKey()))
              .collect(Collectors.toCollection(LinkedList::new))
              .toArray(new String[0]);
      writer.writeNext(data);
      writer.flush();
      Thread.sleep(1000);
    }
  }
Esempio n. 2
0
 // Write master list of point of interest objects out as a CSV file
 public void saveCSVFile(String fileName, List<ParsedPointOfInterest> myList) throws IOException {
   FileOutputStream output = openFileOutput(fileName, MODE_PRIVATE);
   OutputStreamWriter writer = new OutputStreamWriter(output);
   CSVWriter csvWriter = new CSVWriter(writer);
   String columns[];
   for (ParsedPointOfInterest poi : myList) {
     columns = poi.getColumns();
     csvWriter.writeNext(columns);
   }
   writer.close();
   csvWriter.flush();
   csvWriter.close();
 }
Esempio n. 3
0
  private void exportCsvFile(File destFile, ExportConfig exportConfig) throws ExportFileException {
    CSVWriter csvWriter = null;
    try {
      csvWriter = new CSVWriter(new FileWriterWithEncoding(destFile, "gbk"));

      List<Map> list = this.listAllAsMap(exportConfig.getSearchCondition());

      String[] line = null;
      line =
          new String[] {
            "机房名称", "场地号", "模块号", "框号 ", "槽号", "端口号", "设备号", "是否反极", "产品号码", "MDF横列(不准)",
          };
      csvWriter.writeNext(line);

      MapValueGetter mvg = null;
      for (Map map : list) {
        mvg = new MapValueGetter(map);
        line =
            new String[] {
              mvg.getAsString("jfmc"),
              mvg.getAsIntegerString("cdh"),
              mvg.getAsIntegerString("mkh"),
              mvg.getAsIntegerString("kh"),
              mvg.getAsIntegerString("ch"),
              mvg.getAsIntegerString("dkh"),
              mvg.getAsIntegerString("sbh"),
              mvg.getAsTrimedString("fj"),
              mvg.getAsTrimedString("dhhm"),
              mvg.getAsTrimedString("hl"),
            };
        csvWriter.writeNext(line);
      }

      csvWriter.flush();
      csvWriter.close();

    } catch (IOException e) {
      throw new ExportFileException(e);
    } finally {
      if (csvWriter != null) {
        try {
          csvWriter.close();
        } catch (IOException e) {
        }
      }
    }
  }
  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();
  }
  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) {
        for (Map.Entry<String, NamedLocation> entry : locs.entrySet()) {
          NamedLocation warp = entry.getValue();

          csv.writeNext(
              new String[] {
                warp.getName(),
                warp.getWorldName() != null
                    ? warp.getWorldName()
                    : warp.getLocation().getWorld().getName(),
                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 e) {
        }
      }
    }
  }
  public void writeToFile(File file) throws IOException {
    FileWriter fw = new FileWriter(file);
    CSVWriter csv = new CSVWriter(fw);

    csv.writeNext(new String[] {"ego_name", "unused", "alter_number", "alter_name", "group"});

    for (NameMapperFrame.NameMapping mapping : nameMappings) {
      String name = mapping.getInterview().getIntName();
      csv.writeNext(
          new String[] {
            name,
            "",
            mapping.getAlterNumber() + "",
            mapping.toString(), // alter name
            mapping.getGroup() + ""
          });
    }

    csv.flush();
    fw.close();
  }
  @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();
  }
Esempio n. 8
0
 public static void calculateOverlap(String file1, String file2, String outFile)
     throws IOException {
   CSVReader csvReader = new CSVReader(new FileReader(new File(file1)));
   List<String[]> file1Lines = csvReader.readAll();
   csvReader.close();
   csvReader = new CSVReader(new FileReader(new File(file2)));
   List<String[]> file2Lines = csvReader.readAll();
   Map<String, Object> cache = new HashMap<String, Object>();
   csvReader.close();
   for (String[] columns : file2Lines) {
     cache.put(columns[0], null);
   }
   CSVWriter csvWriter = new CSVWriter(new FileWriter(new File(outFile)));
   for (String[] columns : file1Lines) {
     if (cache.containsKey(columns[0])) {
       csvWriter.writeNext(new String[] {columns[0], "MATCH"});
     } else {
       csvWriter.writeNext(new String[] {columns[0], "NO_MATCH"});
     }
   }
   csvWriter.flush();
   csvWriter.close();
 }
  public static void exportCsv(String unitId) throws IOException {

    Phone p = Phone.find("unitId = ?", unitId).first();

    if (p == null) index(true);

    List<Route> routes = Route.find("phone = ?", p).fetch();

    File outputDirectory =
        new File(Play.configuration.getProperty("application.exportDataDirectory"), unitId);
    File outputZipFile =
        new File(
            Play.configuration.getProperty("application.exportDataDirectory"), unitId + ".zip");

    // write routes
    File routesFile = new File(outputDirectory, unitId + "_routes.csv");
    File stopsFile = new File(outputDirectory, unitId + "_stops.csv");

    if (!outputDirectory.exists()) {
      outputDirectory.mkdir();
    }

    if (outputZipFile.exists()) {
      outputZipFile.delete();
    }

    FileWriter routesCsv = new FileWriter(routesFile);
    CSVWriter rotuesCsvWriter = new CSVWriter(routesCsv);

    FileWriter stopsCsv = new FileWriter(stopsFile);
    CSVWriter stopsCsvWriter = new CSVWriter(stopsCsv);

    String[] routesHeader =
        "unit_id, route_id, route_name, route_description, field_notes, vehicle_type, vehicle_capacity, start_capture"
            .split(",");

    String[] stopsHeader =
        "route_id, stop_sequence, lat, lon, travel_time, dwell_time, board, alight".split(",");

    rotuesCsvWriter.writeNext(routesHeader);
    stopsCsvWriter.writeNext(stopsHeader);

    for (Route r : routes) {

      String[] routeData = new String[routesHeader.length];
      routeData[0] = unitId;
      routeData[1] = r.id.toString();
      routeData[2] = r.routeLongName;
      routeData[3] = r.routeDesc;
      routeData[4] = r.routeNotes;
      routeData[5] = r.vehicleType;
      routeData[6] = r.vehicleCapacity;
      routeData[7] = (r.captureTime != null) ? r.captureTime.toGMTString() : "";

      rotuesCsvWriter.writeNext(routeData);

      List<TripPatternStop> stops = TripPatternStop.find("pattern.route = ?", r).fetch();

      for (TripPatternStop s : stops) {

        String[] stopData = new String[stopsHeader.length];

        stopData[0] = r.id.toString();
        stopData[1] = s.stopSequence.toString();
        stopData[2] = "" + s.stop.location.getCoordinate().y;
        stopData[3] = "" + s.stop.location.getCoordinate().x;
        stopData[4] = "" + s.defaultTravelTime;
        stopData[5] = "" + s.defaultDwellTime;
        stopData[6] = "" + s.board;
        stopData[7] = "" + s.alight;

        stopsCsvWriter.writeNext(stopData);
      }
    }

    rotuesCsvWriter.flush();
    rotuesCsvWriter.close();

    stopsCsvWriter.flush();
    stopsCsvWriter.close();

    DirectoryZip.zip(outputDirectory, outputZipFile);
    FileUtils.deleteDirectory(outputDirectory);

    redirect("/public/data/exports/" + unitId + ".zip");
  }
Esempio n. 10
0
        @Override
        public void safeActionPerformed(ActionEvent e) throws Exception {

          String fileName;
          fileName = study.getStudyName() + "_wholenetwork_nodes";
          File currentDirectory = new File(studyFile.getParent() + "/Graphs");
          currentDirectory.mkdir();

          JFileChooser fileChooser = new JFileChooser();
          fileChooser.setFileFilter(new FileNameExtensionFilter("Comma-Separated Values", "csv"));
          fileChooser.setCurrentDirectory(currentDirectory);
          fileChooser.setSelectedFile(new File(fileName + ".csv"));
          fileChooser.setDialogTitle("Save Alter Attributes (CSV)");
          fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

          int returnValue = JFileChooser.APPROVE_OPTION;
          while (returnValue == JFileChooser.APPROVE_OPTION) {
            returnValue = fileChooser.showSaveDialog(parent);
            File dataFile = fileChooser.getSelectedFile();
            try {
              if (dataFile != null && !dataFile.isDirectory()) {
                String path = dataFile.getAbsolutePath();
                if (!path.endsWith(".csv")) {
                  path += ".csv";
                  dataFile = new File(path);
                }

                FileWriter fw = new FileWriter(dataFile);
                CSVWriter csv = new CSVWriter(fw);

                Set<String> questionSet = Sets.newHashSet();
                for (WholeNetworkAlter alter : net.getWholeNetworkAlters().values()) {
                  questionSet.addAll(alter.getAttributes().keySet());
                }
                List<String> questionList = new ArrayList<String>(questionSet);

                List<String> heading = Lists.newArrayList("MappingId", "Name");
                heading.addAll(questionList);
                csv.writeNext(heading.toArray(new String[] {}));

                for (WholeNetworkAlter alter : net.getWholeNetworkAlters().values()) {
                  ArrayList<String> row =
                      Lists.newArrayList(
                          alter.getId() + "",
                          alter
                              .getOccurences()
                              .get(0)
                              .toString()
                              .replaceAll("[^a-zA-Z_\\-0-9]+", "_"));
                  Map<String, String> answers = alter.getAttributes();
                  for (String question : questionList) {
                    String value = answers.get(question);
                    row.add(value == null ? "" : value);
                  }
                  csv.writeNext(row.toArray(new String[] {}));
                }

                csv.flush();
                fw.close();
              }
            } catch (Exception e1) {
              throw new RuntimeException(e1);
            }
            break;
          }
        }
  /** {@inheritDoc} */
  @Override
  public void onDataWriterException(final DataWriterException e) {
    CSVWriter writer = null;
    if (e.getErrorId().equalsIgnoreCase("SINGLE_ROW")) {
      try {
        String path =
            GAPageViewDataExceptionHandler.getErrorFilePath(
                e.getTableDefinition().getProperty("relativePath"),
                e.getTableDefinition().getProperty("startDate"));
        File file = FileSystem.getDefault().getFile("logs/" + path, true, true);
        writer = new CSVWriter(new FileWriter(file, true));
        Map<String, Object> row = (Map<String, Object>) e.getData().get("row");
        Object[] values = row.values().toArray();
        Object[] keys = row.keySet().toArray();
        String[] stringValues = new String[values.length];
        String[] headerValue = new String[keys.length];
        for (int i = 0; i < stringValues.length; i++) {
          stringValues[i] = (values[i] != null) ? values[i].toString() : null;
          headerValue[i] = (keys[i] != null) ? keys[i].toString() : null;
        }
        if (!header) {
          writer.writeNext(headerValue);
          writer.flush();
          header = true;
        }
        String data = stringValues[stringValues.length - 1] + " " + e.getData().get("e");
        stringValues[stringValues.length - 1] = data;
        writer.writeNext(stringValues);
        writer.flush();
        writer.close();
      } catch (Exception ee) {
        logger.error(ee.getMessage());
      }
    } else if (e.getErrorId().equalsIgnoreCase("BATCH_READ")) {
      try {
        String path =
            GAPageViewDataExceptionHandler.getErrorFilePath(
                e.getTableDefinition().getProperty("relativePath"),
                e.getTableDefinition().getProperty("startDate"));
        File file = FileSystem.getDefault().getFile("logs/" + path, true, true);
        writer = new CSVWriter(new FileWriter(file, true));
        String[] row = (String[]) e.getData().get("row");
        String[] headerRow = (String[]) e.getData().get("header");
        if (!header) {
          writer.writeNext(headerRow);
          header = true;
        }

        String data = row[row.length - 1] + e.getData().get("e");
        row[row.length - 1] = data;
        writer.writeNext(row);

        writer.flush();
        writer.close();
      } catch (Exception exceptionObject) {
        logger.error(exceptionObject.getMessage());
      }
    } else if (e.getErrorId().equalsIgnoreCase("ROW_UPDATE")) {
      String[] headerValue = null;
      try {

        String path =
            GAPageViewDataExceptionHandler.getErrorFilePath(
                e.getTableDefinition().getProperty("relativePath"),
                e.getTableDefinition().getProperty("startDate"));
        File file = FileSystem.getDefault().getFile("logs/" + path, true, true);
        writer = new CSVWriter(new FileWriter(file, true));
        Collection<Map<String, Object>> batch =
            (Collection<Map<String, Object>>) e.getData().get("batch");
        super.writerFailedCount = writerFailedCount + batch.size();
        List<String[]> rows = new ArrayList<String[]>(batch.size());
        for (Map<String, Object> row : batch) {
          Object[] values = row.values().toArray();
          Object[] keys = row.keySet().toArray();
          String[] stringValues = new String[values.length];
          headerValue = new String[keys.length];

          for (int i = 0; i < stringValues.length; i++) {
            boolean exceptionFlag = false;
            stringValues[i] = (values[i] != null) ? values[i].toString() : null;
            headerValue[i] = (keys[i] != null) ? keys[i].toString() : null;
          }
          rows.add(stringValues);
        }
        if (!header) {
          writer.writeNext(headerValue);
          writer.flush();
          header = true;
        }

        writer.writeAll(rows);
        writer.flush();
        writer.close();
      } catch (Exception exceptionObject) {
        logger.error(exceptionObject.getMessage(), exceptionObject);
      }
    } else if (e.getErrorId().equalsIgnoreCase("BATCH_UPDATE")) {
      String[] headerValue = null;
      try {
        String path =
            GAPageViewDataExceptionHandler.getErrorFilePath(
                e.getTableDefinition().getProperty("relativePath"),
                e.getTableDefinition().getProperty("startDate"));
        File file = FileSystem.getDefault().getFile("logs/" + path, true, true);
        writer = new CSVWriter(new FileWriter(file, true));
        Collection<Map<String, Object>> batch =
            (Collection<Map<String, Object>>) e.getData().get("batch");
        super.writerFailedCount = writerFailedCount + batch.size();

        List<String[]> rows = new ArrayList<String[]>(batch.size());
        for (Map<String, Object> row : batch) {
          Object[] values = row.values().toArray();
          Object[] keys = row.keySet().toArray();
          String[] stringValues = new String[values.length];
          headerValue = new String[keys.length];

          for (int i = 0; i < stringValues.length; i++) {
            stringValues[i] = (values[i] != null) ? values[i].toString() : null;
            headerValue[i] = (keys[i] != null) ? keys[i].toString() : null;
          }
          rows.add(stringValues);
        }
        if (!header) {
          writer.writeNext(headerValue);
          writer.flush();
          header = true;
        }
        writer.writeAll(rows);
        writer.flush();
        writer.close();
      } catch (IOException e1) {
        logger.warn("GAEventDataExceptionHandler onDataWriterException() " + e.getMessage());
      }
    } else if (e.getErrorId().equalsIgnoreCase("ROW_DUPLICATE")) {
      String[] headerValue = null;
      try {
        String path =
            GAPageViewDataExceptionHandler.getErrorFilePath(
                e.getTableDefinition().getProperty("relativePath"),
                e.getTableDefinition().getProperty("startDate"));
        File file = FileSystem.getDefault().getFile("logs/" + path, true, true);
        writer = new CSVWriter(new FileWriter(file, true));
        Collection<Map<String, Object>> batch =
            (Collection<Map<String, Object>>) e.getData().get("batch");
        super.writerFailedCount = writerFailedCount + batch.size();

        List<String[]> rows = new ArrayList<String[]>(batch.size());
        for (Map<String, Object> row : batch) {
          Object[] values = row.values().toArray();
          Object[] keys = row.keySet().toArray();
          String[] stringValues = new String[values.length];
          headerValue = new String[keys.length];

          for (int i = 0; i < stringValues.length; i++) {
            stringValues[i] = (values[i] != null) ? values[i].toString() : null;
            headerValue[i] = (keys[i] != null) ? keys[i].toString() : null;
          }
          String data = stringValues[stringValues.length - 1] + "  " + e.getData().get("e");
          stringValues[stringValues.length - 1] = data;
          rows.add(stringValues);
        }
        if (!header) {
          writer.writeNext(headerValue);
          writer.flush();
          header = true;
        }

        writer.writeAll(rows);
        writer.flush();
        writer.close();
      } catch (IOException e1) {
        logger.warn("GAEventDataExceptionHandler onDataWriterException() " + e.getMessage());
      }
    }
    super.onDataWriterException(e, false);
  }