Java Code Examples for org.supercsv.io.ICsvBeanReader#read()

The following examples show how to use org.supercsv.io.ICsvBeanReader#read() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AirlineCsvDeserializer.java    From Showcase with Apache License 2.0 6 votes vote down vote up
public List<Airline> deserialize(InputStream in) throws IOException {
    InputStreamReader inputStreamReader = new InputStreamReader(in);
    ICsvBeanReader csvBeanReader = new CsvBeanReader(inputStreamReader, CsvPreference.STANDARD_PREFERENCE);
    final String[] header = {null, "name", "alias", "iataCode", "icaoCode", "callsign", "country", null};
    Airline airline = null;
    List<Airline> airlines = new ArrayList<>();
    try {

        while ((airline = csvBeanReader.read(Airline.class, header, getCellProcessors())) != null) {
            System.out.println("deserialized " + airline);
            airlines.add(airline);
        }
    } finally {
        if (csvBeanReader != null) {
            csvBeanReader.close();
        }
    }
    return airlines;
}
 
Example 2
Source File: AirportCsvDeserializer.java    From Showcase with Apache License 2.0 6 votes vote down vote up
public List<Airport> deserialize(InputStream in) throws IOException {
    InputStreamReader inputStreamReader = new InputStreamReader(in);
    ICsvBeanReader csvBeanReader = new CsvBeanReader(inputStreamReader, CsvPreference.STANDARD_PREFERENCE);
    final String[] header = {null, "name", "city", "country", "iataCode", "icaoCode", "latitude", "longitude", null, null, null, null, null, null};
    Airport airport = null;
    List<Airport> airports = new ArrayList<>();
    try {

        while ((airport = csvBeanReader.read(Airport.class, header, getCellProcessors())) != null) {
            System.out.println("deserialized " + airport);
            airports.add(airport);
        }
    } finally {
        if (csvBeanReader != null) {
            csvBeanReader.close();
        }
    }
    return airports;
}
 
Example 3
Source File: ConnectionCsvDeserializer.java    From Showcase with Apache License 2.0 6 votes vote down vote up
public List<FlightConnection> deserialize(InputStream in) throws IOException {
    InputStreamReader inputStreamReader = new InputStreamReader(in);
    ICsvBeanReader csvBeanReader = new CsvBeanReader(inputStreamReader, CsvPreference.STANDARD_PREFERENCE);
    final String[] header = {"airlineIataCode", null, "sourceAirportIataCode", null, "destinationAirportIataCode", null, "codeshare", "stops", null};
    FlightConnection flightConnection = null;
    List<FlightConnection> flightConnections = new ArrayList<>();
    try {

        while ((flightConnection = csvBeanReader.read(FlightConnection.class, header, getCellProcessors())) != null) {
            System.out.println("deserialized " + flightConnection);
            flightConnections.add(flightConnection);
        }
    } finally {
        if (csvBeanReader != null) {
            csvBeanReader.close();
        }
    }
    return flightConnections;
}
 
Example 4
Source File: SuperCsvBOMTest.java    From super-csv with Apache License 2.0 6 votes vote down vote up
public void ReadTestCSVFile(Reader reader) throws IOException {
	ICsvBeanReader beanReader = new CsvBeanReader(reader, CsvPreference.STANDARD_PREFERENCE);
	final String[] header = beanReader.getHeader(true);
	assertEquals("customerNo", header[0]);
	CustomerBean customer = null;
	final String emailRegex = "[a-z0-9\\._]+@[a-z0-9\\.]+"; // just an example, not very robust!
	StrRegEx.registerMessage(emailRegex, "must be a valid email address");
	final CellProcessor[] processors = new CellProcessor[]{new UniqueHashCode(), // customerNo (must be unique)
			new NotNull(), // firstName
			new NotNull(), // lastName
			new ParseDate("dd/MM/yyyy"), // birthDate
			new ParseSqlTime("HH:mm:ss"), // birthTime
			new NotNull(), // mailingAddress
			new Optional(new ParseBool()), // married
			new Optional(new ParseInt()), // numberOfKids
			new NotNull(), // favouriteQuote
			new StrRegEx(emailRegex), // email
			new LMinMax(0L, LMinMax.MAX_LONG) // loyaltyPoints
	};
	customer = beanReader.read(CustomerBean.class, header, processors);
	assertEquals("1", customer.getCustomerNo());
	assertEquals("John", customer.getFirstName());
	assertEquals("[email protected]", customer.getEmail());
	assertEquals(0, customer.getLoyaltyPoints());
	beanReader.close();
}
 
Example 5
Source File: CSVImporter.java    From robe with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void importStream(InputStream inputStream, OnItemHandler handler, String charSetName) throws Exception {

    Reader reader = new InputStreamReader(inputStream, charSetName);

    ICsvBeanReader csvBeanReader = new CsvBeanReader(reader, this.preference);
    Object obj;
    while ((obj = csvBeanReader.<T>read(getDataClass(), this.fieldNames, this.processors)) != null) {
        handler.onItem(obj);
    }

}