Skip to main content

How to convert a Java Object List into csv.

Wrote a general method to convert a List into a CSV file.



package com.biplav.utils



import java.lang.reflect.Field;

import java.lang.reflect.Modifier;

import java.util.ArrayList;

import java.util.List;



import org.apache.log4j.Logger;



public class ObjectListToCSV {



private static final Logger logger = Logger.getLogger(ObjectListToCSV.class);

private static final String CSV_SEPARATOR = ",";



public static  String convertListToCSV(List objectList) {
  if(objectList.size() < 1) {
   logger.info("No data in the list to convert to CDR!");
   return "";
  }
  String csv = "";
  T t = objectList.get(0);
  Field[] declaredFields = t.getClass().getDeclaredFields();
  ArrayList useableFields = getUseableFields(declaredFields);
  csv = getCSVHeader(csv, useableFields,null);
  csv=csv.concat("\n");
  for(T object : objectList) {
   csv = addObjectValue(csv, useableFields, object);
   csv=csv.concat("\n");
  }
  return csv;
 }

 private static  String addObjectValue(String csv,
   ArrayList useableFields, T object) {
  for(Field field : useableFields) {
   try {
    if(canFieldUsedDirectly(field)) {
     if(object != null) {
      field.setAccessible(true);
      Object value = field.get(object);
      csv=csv.concat(value +"");
      field.setAccessible(false);
     }
    } else {
     if(object != null) {
      field.setAccessible(true);
      Object value = field.get(object);
      field.setAccessible(false);
      csv = addObjectValue(csv, getUseableFields(field.getType().getDeclaredFields()), value);
     }
    }
   } catch (IllegalArgumentException e) {
    logger.error(e);
   } catch (SecurityException e) {
    logger.error(e);
   } catch (IllegalAccessException e) {
    logger.error(e);
   }
   csv=csv.concat(CSV_SEPARATOR);
  }
  return csv;
 }

 private static boolean canFieldUsedDirectly(Field field) {
  return !field.getType().toString().contains("biplav");
 }

 private static ArrayList getUseableFields(Field[] declaredFields) {
  ArrayList useableFields = new ArrayList();
  for(Field field: declaredFields) {
   if(!Modifier.isStatic(field.getModifiers())) {
     useableFields.add(field);
   }
  }
  return useableFields;
 }

 private static String getCSVHeader(String csv, ArrayList useableFields,String prefix) {
  prefix = (prefix == null) ?  "": prefix;
  for(Field field : useableFields) {
   if(canFieldUsedDirectly(field)) {
    csv=csv.concat(prefix+field.getName());
    csv=csv.concat(CSV_SEPARATOR);
   } else {
    csv = getCSVHeader(csv, getUseableFields(field.getType().getDeclaredFields()),field.getName()+"_");
   }
  }
  return csv;
 }
}

Popular posts from this blog

Watch Live cam on Google!!!!!

Ahhh!!! type certain string in google search bar above and it would bring up the network live cam into your browser. These can be anything from CCTV or webcams... There are lots of string.. i suggest a few down below use them to begin with.. And do come up with your own.. and leave a comment to the post... And ya.. if u come up with something interesting then don forget to share it.. Strings::: Axis cameras: "adding live video to one of your own pages a very easy task with an AXIS 2100 Network Camera" ' google ' intile:"Live view - / - AXIS" ' google ' "Your browser has JavaScript turned off.For the user interface to work effectively" ' google ' inurl:indexFrame.html axis ' google ' "Live web imaging unleashed" ' google ' MOBOTIX cameras: (intext:"MOBOTIX M1" | intext:"MOBOTIX M10") intext:"Open Menu" Shift-Reload ' google ' JVC cameras: "(c)copyright 199...

Breaking open https://web.whatsapp.com/

Today whatsapp have launched an online/web version of their overly popular smartphone messaging app. I was very much interested in seeing the architecture of this app because as far as i knew, they never stored messages on their server but all the data was only stored in users phone. So I started to look under the hood of the webapp and what I saw was a beauty. First let me list down the frameworks they have used in creating this app: React .js : A JAVASCRIPT LIBRARY FOR BUILDING USER INTERFACES from Facebook. Underscore.js  : Unerscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects. It’s the answer to the question: “If I sit down in front of a blank HTML page, and want to start being productive immediately, what do I need?” … and the tie to go along with jQuery's tux and Backbone's suspenders. Velocity.js : Velocity is an animation engine with the same API as jQuery's $.animate(...

Why India Hasn’t Built Its GPT Moment (Yet)

India has the world’s third-largest startup ecosystem, a thriving developer base, and a mobile-first population larger than the US and Europe combined. Yet, no GPT-4. No DeepMind. No Amazon-style platform. Why? Innovation Isn’t Accidental—It’s Engineered The Zerodha Daily Brief recently asked why India hasn’t built a global product company like Apple. The key argument: India isn’t building for the world. It’s solving for local constraints, scale, and affordability—but global scale requires deep IP, design, and tech differentiation. It’s not just about software, it’s about systems thinking. More importantly, it answers the question: Why do countries innovate? The answer isn’t just genius or ambition—it’s incentives and ecosystems. The U.S. Defense Department, for example, accounted for nearly 70% of federal R&D funding during the Cold War. China has pumped billions into semiconductors and AI with long-term national alignment. These aren’t short-term bets—they are strategic, delibe...