Frage

We are using OpenCSV to map CSV file to Java Bean. I want to make all String values to uppar case while mapping it to Java bean.
Suppose CSV contains following line

abc, 123, def, 123abc   

while mapping it to bean it should be converted

ABC, 123, DEF, 123abc

I can go with reflection and have the desired result .But was looking for a solution through OpenCSV or Is there any feature in Apache Bean Utils or any other similar library which converts all String fields from a bean to uppercase?

War es hilfreich?

Lösung

Use an overridden CsvToBean implementation:

CsvToBean<BeanType> csv2Bean = new CsvToBean<BeanType>() {

    @Override
    protected Object convertValue(String value, PropertyDescriptor prop) {
        return super.convertValue(value.toUpperCase(), prop);
    }
};

List<BeanType> beanList = csv2Bean.parse(mappingStrategy, csvReader);

Andere Tipps

Try this, StringUtils.upperCase

if (StringUtils.isAlpha(value)) 
        {
            System.out.print("With uppercase " + StringUtils.upperCase(value));
        }
        else  // with number or anything with the word
        {
            System.out.print("Without uppercase " + value);
        }

or

System.out.println(""+(StringUtils.isAlpha(value) ? StringUtils.upperCase(value) : value));
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top