/* (non-Javadoc) * @see net.sourceforge.squirrel_sql.plugins.dataimport.importer.IFileImporter#getPreview(int) */ public String[][] getPreview(int noOfLines) throws IOException { CsvReader csvReader = new CsvReader( new InputStreamReader(new FileInputStream(importFile), settings.getImportCharset()), settings.getSeperator()); String[][] data = new String[noOfLines][]; int row = 0; int columns = -1; while (csvReader.readRecord() && row < noOfLines) { if (columns == -1) { columns = csvReader.getColumnCount(); } data[row] = new String[columns]; for (int i = 0; i < columns; i++) { data[row][i] = csvReader.get(i); } row++; } csvReader.close(); String[][] outData = new String[row][]; for (int i = 0; i < row; i++) { outData[i] = data[i]; } return outData; }
/** * Reads floating values from a CSV file and returns them in an array. * * @param state * @param terminalFileCSV the file with the float values * @return an array with all of the float values in the file */ public double[] getRawTimeSeriesValuesFromCSVfile( EvolutionState state, CsvReader terminalFileCSV) { // expect the CSV file to contain only float values ... Vector rawValues = new Vector(100); try { while (terminalFileCSV.readRecord()) { for (int i = 0; i < terminalFileCSV.getColumnCount(); i++) rawValues.add(terminalFileCSV.get(i)); } } catch (IOException e) { state.output.fatal("The file with time series raw values failed when reading records. " + e); } // convert the vector into an array double rvArray[] = new double[rawValues.size()]; for (int i = 0; i < rvArray.length; i++) rvArray[i] = Double.parseDouble((String) rawValues.elementAt(i)); return rvArray; }
private static void filterCSV( String filename, LinkedList<String> filters, LinkedList<String> includes) throws IOException { FileReader fr = new FileReader(filename); CsvReader csvIn = new CsvReader(fr, SEPARATOR); csvIn.setSafetySwitch(false); if (csvIn.readHeaders()) { csvIn.readRecord(); System.out.println("'" + filename + "' has " + csvIn.getColumnCount() + " column."); int usedColumn = 0; String[] headers = csvIn.getHeaders(); StringBuffer newHeader = new StringBuffer(); StringBuffer newValues = new StringBuffer(); HashMap<String, String> data = new HashMap<String, String>(); allData.put(filename, data); for (String header : headers) { boolean matches = false; // check if a filter matches the entry for (String filter : filters) { if (header.contains(filter)) { matches = true; // ok, filter matches, but maybe it is on the include list? for (String include : includes) { if (header.contains(include)) { matches = false; break; } } break; } } if (!matches) { usedColumn++; String value = csvIn.get(header); newHeader.append(header); newHeader.append(SEPARATOR_OUT); newValues.append(value); newValues.append(SEPARATOR_OUT); if (data != null) { if (!keys.containsKey(header)) { keys.put(header, true); } data.put(header, value); } } } System.out.println(" -> " + usedColumn + " column remains"); FileWriter fw = new FileWriter(filename + FILENAME_POSTFIX, false); fw.write(newHeader.toString()); fw.write(NEW_LINE); fw.write(newValues.toString()); fw.close(); } else { System.err.println("Can not read header from '" + filename + "'"); } }