Exemplo n.º 1
0
  /**
   * Importiert CSVPortable Objekte von der angegebenen Datei und Klasse.
   *
   * @param fileName
   * @param type Klasse der tatsaechlichen CSVPortable-Implementierung.
   * @return
   */
  public static CSVPortable[] importFrom(String fileName, Class type) {
    try {
      CSVProperties csv = new CSVProperties();
      csv.load(fileName);

      CSVPortable[] ptbl = new CSVPortable[csv.size()];
      String[] columnNames = null;

      for (int n = 0; n < csv.size(); n++) {
        ptbl[n] = (CSVPortable) type.newInstance();
        if (columnNames == null) columnNames = ptbl[n].getColumnNames();

        String[] row = new String[columnNames.length];

        for (int m = 0; m < columnNames.length; m++) {
          row[m] = csv.get(n, m);
        }

        ptbl[n].setRow(row);
      }
      return ptbl;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
Exemplo n.º 2
0
  /**
   * Exportiert CSVPortable Objekte in die angegebene CSV-Datei.
   *
   * @param fileName
   * @param data
   * @return
   */
  public static boolean exportTo(String fileName, CSVPortable[] data) {
    try {
      String[] columnNames = data[0].getColumnNames();
      CSVProperties prop = new CSVProperties(columnNames);

      for (int n = 0; n < data.length; n++) {
        for (int m = 0; m < columnNames.length; m++) prop.set(n, data[n].getRow());
      }

      prop.save(fileName);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }