public static void main(String... args) { CsvParserSettings parserSettings = new CsvParserSettings(); parserSettings.detectFormatAutomatically(); List<String[]> rows = new CsvParser(parserSettings).parseAll(new StringReader("Amount,Tax,Total\n1.99,10.0,2.189\n5,20.0,6")); for (Object[] row : rows) { System.out.println(Arrays.toString(row)); } System.out.println("####"); rows = new CsvParser(parserSettings).parseAll(new StringReader("Amount;Tax;Total\n1,99;10,0;2,189\n5;20,0;6")); for (Object[] row : rows) { System.out.println(Arrays.toString(row)); } }
CsvParserSettings config = new CsvParserSettings(); //configure what you need by hand, or just do this: config.detectFormatAutomatically(); CsvRoutines csv = new CsvRoutines(config); File input = new File("C:\\Users\\MyName\\Documents\\MyApplication\\app\\src\\main\\res\\raw\\geo_locations.csv"); Map<String, GeoLocation> locations = new LinkedHashMap<String, GeoLocation>(); for(GeoLocation geo : csv.iterate(GeoLocation.class, input)){ locations.put(geo.city, geo); }
CsvParserSettings parserSettings = new CsvParserSettings(); //many options here, check the tutorial. parserSettings.detectFormatAutomatically(); CsvParser parser = new CsvParser(parserSettings); //parse and get the detected format List<String[]> parsedRows = parser.parseAll(new File("/path/to/input.csv")); CsvFormat detectedFormat = parser.getDetectedFormat(); //now, write using the detected format: CsvWriterSettings writerSettings = new CsvWriterSettings(); writerSettings.setFormat(detectedFormat); CsvWriter writer = new CsvWriter(new File("/path/to/output.csv"), writerSettings); writer.writeStringRowsAndClose(parsedRows);
CsvParserSettings settings = new CsvParserSettings(); settings.detectFormatAutomatically(); CsvParser parser = new CsvParser(settings); parser.beginParsing(fileNameWithLocation); String nextLine[]; while ((nextLine = parser.parseNext()) != null){ //do stuff }
CsvParserSettings settings = new CsvParserSettings(); //many options here, check the tutorial. // turns on automatic detection of line separators, // column separators, quotes & quote escapes settings.detectFormatAutomatically(); // configures to skip a number of rows from the input and start parsing from there settings.setNumberOfRowsToSkip(3); // configures the parser to extract headers from the first parsed row settings.setHeaderExtractionEnabled(true); CsvParser parser = new CsvParser(settings); List<String[]> rows = parser.parseAll(new File("/path/to/your/file.csv"));