Вопрос

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?

Это было полезно?

Решение

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);

Другие советы

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));
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top