0

I'm decrypting a string to reveal multiple comma separated values and I want to access the individual pieces of information, but the encrypted string does not include the header values.

I am aware that when reading CSV files information at a specific column can be accessed by using for example:

CSVRecord.get(ID);

Where ID is a header value in the table, but there doesn't seem to be a way to access this without knowing the header name.

The encrypted string is being decrypted in another application.

Is there a way to read the access the comma separated values without knowing the header value, or are there any alternatives to CSVRecord?

cryptiblinux
  • 29
  • 3
  • 12
  • Why is this tagged encryption and license-key? – Robert Mar 20 '17 at 00:28
  • You could access fields by index. https://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVRecord.html#get-int- – Robert Mar 20 '17 at 00:29
  • Removed, but added because the purpose of this task is to get license information from an decrypted licence key – cryptiblinux Mar 20 '17 at 00:35
  • Well, maybe, but your question is all about CSV. You don't ask about encrypting or decrypting, and it does not matter what the CSV data is for. You wouldn't tag this "Linux" if you happen to run it on Linux, either, right? – Robert Mar 20 '17 at 00:37

2 Answers2

1

You can simply iterate over the resulting records and access the i-th item of a record via record.get(i), e.g.

    Reader in = new StringReader(
            "DAR_123451                  ,\"XXXXX Hello World Hello World XXX \"\n" +
            "DAR_123452                  ,\"XXXXX Hello World Hello World XXX \"");
    Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(in);
    for (CSVRecord record : records) {
        for (int i = 0; i < record.size(); i++) {
            System.out.println("At " + i + ": " + record.get(i));
        }
    }
centic
  • 15,565
  • 9
  • 68
  • 125
0

You can access the column names (keys and values) of a CSVRecord by doing:

private void printRecords(final CSVParser csvRecords) {
    for (CSVRecord csvRecord : csvRecords) {
        for (Entry<String, String> column : csvRecord.toMap().entrySet()) {
            System.out.println(column.getKey() + "=" + column.getValue());
        }
    }
}

Alternatively you can iterate over CSVRecord in the same way like over java.util.ArrayList.