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