public static void writeCSV ( DataObjectModel model , Collection < ? extends DataObject > collection , OutputStream os ) throws IOException { BufferedOutputStream bos = new BufferedOutputStream ( os ) ; OutputStreamWriter osw = new OutputStreamWriter ( bos , "ISO-8859-1" ) ; CSVWriter writer = new CSVWriter ( osw , ' ' , ' ' , "\r\n" ) ; String [ ] line = model . getProperties ( ) ; writer . writeNext ( line ) ; for ( DataObject dob : collection ) { int index = 0 ; for ( String property : model . getPropertyList ( ) ) { Object value = dob . get ( property ) ; if ( value != null ) { line [ index ++ ] = value . toString ( ) ; } else { line [ index ++ ] = "" ; } } writer . writeNext ( line ) ; } writer . flush ( ) ; }
Writes collection of DataObjects in csv format . OutputStream is not closed after operation .