Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It would be interesting to have a csv serializer as it would take up the minimal space compared to other serializing method.</p> <p>The closest support for java object to csv is stringutils provided by spring utils project</p> <p>arrayToCommaDelimitedString(Object[] arr) but it is far from being a serializer.</p> <p>Here is a simple utility which uses reflection to serialize value objects</p> <pre><code>public class CSVWriter { private static String produceCsvData(Object[] data) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { if(data.length==0) { return ""; } Class classType = data[0].getClass(); StringBuilder builder = new StringBuilder(); Method[] methods = classType.getDeclaredMethods(); for(Method m : methods) { if(m.getParameterTypes().length==0) { if(m.getName().startsWith("get")) { builder.append(m.getName().substring(3)).append(','); } else if(m.getName().startsWith("is")) { builder.append(m.getName().substring(2)).append(','); } } } builder.deleteCharAt(builder.length()-1); builder.append('\n'); for(Object d : data) { for(Method m : methods) { if(m.getParameterTypes().length==0) { if(m.getName().startsWith("get") || m.getName().startsWith("is")) { System.out.println(m.invoke(d).toString()); builder.append(m.invoke(d).toString()).append(','); } } } builder.append('\n'); } builder.deleteCharAt(builder.length()-1); return builder.toString(); } public static boolean generateCSV(File csvFileName,Object[] data) { FileWriter fw = null; try { fw = new FileWriter(csvFileName); if(!csvFileName.exists()) csvFileName.createNewFile(); fw.write(produceCsvData(data)); fw.flush(); } catch(Exception e) { System.out.println("Error while generating csv from data. Error message : " + e.getMessage()); e.printStackTrace(); return false; } finally { if(fw!=null) { try { fw.close(); } catch(Exception e) { } fw=null; } } return true; } </code></pre> <p>}</p> <p>Here is an example value object </p> <pre><code>public class Product { private String name; private double price; private int identifier; private boolean isVatApplicable; public Product(String name, double price, int identifier, boolean isVatApplicable) { super(); this.name = name; this.price = price; this.identifier = identifier; this.isVatApplicable = isVatApplicable; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(long price) { this.price = price; } public int getIdentifier() { return identifier; } public void setIdentifier(int identifier) { this.identifier = identifier; } public boolean isVatApplicable() { return isVatApplicable; } public void setVatApplicable(boolean isVatApplicable) { this.isVatApplicable = isVatApplicable; } </code></pre> <p>} </p> <p>and the code to run the util</p> <pre><code>public class TestCSV { public static void main(String... a) { Product[] list = new Product[5]; list[0] = new Product("dvd", 24.99, 967, true); list[1] = new Product("pen", 4.99, 162, false); list[2] = new Product("ipad", 624.99, 234, true); list[3] = new Product("crayons", 4.99,127, false); list[4] = new Product("laptop", 1444.99, 997, true); CSVWriter.generateCSV(new File("C:\\products.csv"),list); } } </code></pre> <p>Output:</p> <pre><code>Name VatApplicable Price Identifier dvd true 24.99 967 pen false 4.99 162 ipad true 624.99 234 crayons false 4.99 127 laptop true 1444.99 997 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload