@Override
 public void exportCSV(String outputFile) {
   List<Schedule> data = getSchedule();
   FileWriter fileWriter = null;
   CSVPrinter csvFilePrinter = null;
   try {
     fileWriter = new FileWriter(outputFile);
     csvFilePrinter = new CSVPrinter(fileWriter, CSVFormat.DEFAULT.withRecordSeparator("\n"));
     for (int i = 0; i < data.size(); i++) {
       List<Object> line = new ArrayList<>();
       for (Field field : Schedule.class.getDeclaredFields()) {
         field.setAccessible(true);
         Object value = field.get(data.get(i));
         line.add(value);
       }
       csvFilePrinter.printRecord(line);
     }
   } catch (IOException | IllegalAccessException e) {
     e.printStackTrace();
   } finally {
     try {
       if (fileWriter != null) {
         fileWriter.flush();
         fileWriter.close();
       }
       if (csvFilePrinter != null) {
         csvFilePrinter.close();
       }
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
 }
  public static void main(String[] args) {
    // 要导入数据库的文档
    List<Document> docList = new ArrayList<Document>();
    File inFile = new File(IN_PATH);
    CSVFormat format = CSVFormat.DEFAULT.withRecordSeparator('\n'); // 每条记录间隔符
    if (!inFile.exists()) {
      throw new RuntimeException("原始输入文件不存在!");
    }

    Reader reader = null;
    CSVParser parser = null;
    try {
      reader = new InputStreamReader(new FileInputStream(inFile), "utf-8");
      parser = new CSVParser(reader, format);
    } catch (IOException e) {
      throw new RuntimeException("Csv File output preparing fails", e);
    }

    Iterator<CSVRecord> iterator = parser.iterator();
    while (iterator.hasNext()) {
      CSVRecord record = iterator.next();
      Iterator<String> iterator2 = record.iterator();
      boolean isTarget = true;
      String target = null;
      while (iterator2.hasNext()) {
        if (isTarget) {
          target = iterator2.next();
          isTarget = false;
          continue;
        }
        String[] recAndScore = iterator2.next().trim().split("\\|");
        if (recAndScore.length == 3) {
          String rec = recAndScore[0];
          String score = recAndScore[1];
          String cosine = recAndScore[2];

          Map<String, Object> map = new HashMap<String, Object>();
          map.put("target", target);
          map.put("rec", rec);
          map.put("score", Double.valueOf(score));
          map.put("cosine", Double.valueOf(cosine));
          docList.add(new Document(map));
        }
      }
    }

    System.out.println(docList.size());

    try {
      parser.close();
      reader.close();
    } catch (IOException e) {
      e.printStackTrace();
    }

    importToMongoDB(MONGO_HOST, MONGO_PORT, DATABASE, COLLECTION, docList);
  }
 public TradingDataResource(final URL locatorBase, final String strategy) {
   super("Trading Data", locatorBase);
   this.strategy = strategy;
   this.csvFormat =
       CSVFormat.DEFAULT
           .withRecordSeparator(
               ApplicationContext.getInstance()
                   .getConfigurationResource()
                   .getDefaultCsvRecordSeparator())
           .withHeader();
 }
  public void crearCSV(LinkedList<Mapeo> rsList) throws IOException {
    FileWriter escritorArchivo = null;
    CSVPrinter csvPrinter = null;

    CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(SALTO_LINEA);
    escritorArchivo = new FileWriter(PATH_VOLCADO_DB);
    csvPrinter = new CSVPrinter(escritorArchivo, csvFileFormat);
    csvPrinter.printRecord(CABECERAS);

    for (Mapeo elemento : rsList) {
      List<String> camposCsv = new ArrayList<String>();
      camposCsv.add(elemento.getIdERP());
      camposCsv.add(elemento.getIdNimbus());
      camposCsv.add(elemento.getDescERP());
      camposCsv.add(elemento.getDescNimbus());
      csvPrinter.printRecord(camposCsv);
    }

    escritorArchivo.flush();
    escritorArchivo.close();
    csvPrinter.close();
  }
  @Override
  public String outputReportResult(Report report) {

    FileWriter fileWriter = null;
    CSVPrinter csvFilePrinter = null;
    CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR);

    try {
      SimpleDateFormat dateFormat = new SimpleDateFormat("ddmmyyHHmmss");
      String outputFileName = String.format(OUTPUT_FILE_NAME, dateFormat.format(new Date()));
      fileWriter = new FileWriter(outputFileName);
      csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);
      csvFilePrinter.printRecord(FILE_HEADER);

      for (ReportList reportList : report.getReportLists()) {
        csvFilePrinter.printRecord(
            reportList.getPerson(),
            reportList.getPaymentMonth(),
            reportList.getGrossIncome(),
            reportList.getIncomeTax(),
            reportList.getNetIncome(),
            reportList.getSuperannuation());
      }

      return outputFileName;

    } catch (Exception e) {
      e.printStackTrace();
      return null;
    } finally {
      try {
        fileWriter.flush();
        fileWriter.close();
        csvFilePrinter.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
 @Override
 public void importCSV(String inputFile) {
   try {
     String csvData = new String(Files.readAllBytes(FileSystems.getDefault().getPath(inputFile)));
     csvData = csvData.replaceAll("\\r", "");
     CSVParser parser = CSVParser.parse(csvData, CSVFormat.DEFAULT.withRecordSeparator("\n"));
     for (CSVRecord record : parser) {
       Schedule schedule = new Schedule();
       schedule.setId(Integer.parseInt(record.get(0)));
       schedule.setMatchNum(Integer.parseInt(record.get(1)));
       schedule.setB1(Integer.parseInt(record.get(2)));
       schedule.setB2(Integer.parseInt(record.get(3)));
       schedule.setB3(Integer.parseInt(record.get(4)));
       schedule.setR1(Integer.parseInt(record.get(5)));
       schedule.setR2(Integer.parseInt(record.get(6)));
       schedule.setR3(Integer.parseInt(record.get(7)));
       if (checkForMatch(schedule)) update(schedule);
       else create(schedule);
     }
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
Example #7
0
 @Test
 public void testWithRecordSeparatorCRLF() throws Exception {
   final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator(CRLF);
   assertEquals(CRLF, formatWithRecordSeparator.getRecordSeparator());
 }
Example #8
0
 @Test
 public void testWithRecordSeparatorLF() throws Exception {
   final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator(LF);
   assertEquals(String.valueOf(LF), formatWithRecordSeparator.getRecordSeparator());
 }