Thursday, 15 August 2013

java - Writing multiple objects to csv 1 per line -



java - Writing multiple objects to csv 1 per line -

im trying write serialize object multiline csv file. instead of 1 per line writes in 1 line.

here code.

purchaseorder po = new purchaseorder(entry); arraylist<object> l = lists.newarraylist(); l.add(po); l.add(new purchaseorder(entry)); csvmapper mapper = new csvmapper(); csvschema schema = mapper.schemafor(purchaseorder.class); schema = schema.withcolumnseparator(','); objectwriter author = mapper.writer(schema); file tempfile = new file("test.csv"); fileoutputstream tempfileoutputstream = new fileoutputstream(tempfile); bufferedoutputstream bufferedoutputstream = new bufferedoutputstream(tempfileoutputstream, 1024); outputstreamwriter outputstreamwriter = new outputstreamwriter(bufferedoutputstream, "utf-8"); for(object o : l) { writer.writevalue(outputstreamwriter, o); }

the reason arraylist end result needs generic arraylist need able contain ant type of object.

this solution worked in end. passing in list way it

purchaseorder po = new purchaseorder(entry); arraylist<object> l = lists.newarraylist(); l.add(po); l.add(new purchaseorder(entry)); csvmapper mapper = new csvmapper(); csvschema schema = mapper.schemafor(purchaseorder.class); schema = schema.withcolumnseparator(','); objectwriter author = mapper.writer(schema); file tempfile = new file("test.csv"); fileoutputstream tempfileoutputstream = new fileoutputstream(tempfile); bufferedoutputstream bufferedoutputstream = new bufferedoutputstream(tempfileoutputstream, 1024); outputstreamwriter outputstreamwriter = new outputstreamwriter(bufferedoutputstream, "utf-8"); writer.writevalue(outputstreamwriter, l);

-------new problem------- above sollution works if same object. 2 different object each needing own line schema has alter proving more troublesome thought be.

here im trying @ moment without success.

arraylist<object> l = lists.newarraylist(); l.add(new purchaseordere(entry)); l.add(new purchaseorderl(entry)); csvmapper mapper = new csvmapper(); //csvschema schema = mapper.schemafor(purchaseordere.class); //schema = schema.withcolumnseparator(','); //schema = schema.withlineseparator(""); //objectwriter author = mapper.writer(schema); file tempfile = new file("test.csv"); //fileoutputstream tempfileoutputstream = new fileoutputstream(tempfile); //bufferedoutputstream bufferedoutputstream = new bufferedoutputstream(tempfileoutputstream, 1024); //outputstreamwriter outputstreamwriter = new outputstreamwriter(bufferedoutputstream, "utf-8"); //for(object o : l) { //csvschema schema = mapper.schemafor(o.getclass()); //schema = schema.withcolumnseparator(','); //schema = schema.withlineseparator(""); objectwriter author = mapper.writer(); fileoutputstream tempfileoutputstream = new fileoutputstream(tempfile); bufferedoutputstream bufferedoutputstream = new bufferedoutputstream(tempfileoutputstream, 1024); outputstreamwriter outputstreamwriter = new outputstreamwriter(bufferedoutputstream, "utf-8"); writer.writevalue(outputstreamwriter, l); //}

the pieces commited out have tried sofar without success. there has im missing cnt think great lib wnt provide way serialize different objects same file.

java csv serialization jackson

No comments:

Post a Comment