Domanda

Here is an example of csv file:

customerNo,firstName,lastName,birthDate,mailingAddress,married,numberOfKids,favouriteQuote,email,loyaltyPoints

1,John,Dunbar,13/06/1945,"1600 Amphitheatre Parkway Mountain View, CA 94043 United States",,,"""May the Force be with you."" - Star Wars",jdunbar@gmail.com,0
2,Bob,Down,25/02/1919,"1601 Willow Rd. Menlo Park, CA 94025 United States",Y,0,"""Frankly, my dear, I don't give a damn."" - Gone With The Wind",bobdown@hotmail.com,123456

For exmaple, How can I get the all the column values belong to lastName?
I expect the result should be [Dunbar, Down].

I've tried to search the doc, but could not find any useful example.

One more question is that, what's the meaning of getLineNumber() method? I've read the docu and it says "Gets the current position in the file. The first line of the file is line number 1." But I am still confusing.

È stato utile?

Soluzione

You have a number of options. I've used CsvListReader, but you could just as easily use CsvMapReader or CsvBeanReader instead.

You could use the Collector cell processor:

private static final String CSV = "firstName,lastName\nJohn,Dunbar\nBob,Down";

@Test
public final void testCollector() throws IOException {
    ICsvListReader reader = new CsvListReader(new StringReader(CSV), 
        CsvPreference.STANDARD_PREFERENCE);
    reader.getHeader(true); // skip header

    // Collector processor 'collects' values from a column
    List<Object> firstNames = new ArrayList<Object>();
    CellProcessor[] processors = {new Collector(firstNames), null};

    while(reader.read(processors) != null){
        // just keep reading - Collector will collect the values
    }

    // prints: [John, Bob]
    System.out.println(firstNames);
}

Or grab the relevant column after Super CSV has read each line:

@Test
public final void testManual() throws IOException {
    ICsvListReader reader = new CsvListReader(new StringReader(CSV), 
        CsvPreference.STANDARD_PREFERENCE);
    reader.getHeader(true); // skip header

    List<String> firstNames = new ArrayList<String>();

    List<String> line;
    while((line = reader.read()) != null){
        firstNames.add(line.get(0));
    }

    // prints: [John, Bob]
    System.out.println(firstNames);
}

Either way you'll need to read the whole file - CSV is not a spreadsheet and you can't just grab all of the values in a particular column easily (records can span more than 1 line!). Which brings me to your next question...

Straight from the javadoc:

getLineNumber(): Gets the current position in the file. The first line of the file is line number 1.

getRowNumber(): Gets the current row number (i.e. the number of CSV records - including the header - that have been read). This differs from the lineNumber, which is the number of real lines that have been read in the file. The first row is row 1 (which is typically the header row).

Altri suggerimenti

If you can read each line of the csv e.g with readline, just split the string and read column 2.

String[] csvLine = getLine().split(",");  
String lastName = csvLine[2];  
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top