java - PrintWriter not appending content in the right order -


i have list contains objects (whose constructor contains inner object). when i'm trying print list file, go through each object , call object's respective writer methods.

public void writer(string file, boolean append) {     file path = new file("../opdracht6_2/src/" + file);     try {         printwriter write = new printwriter(new fileoutputstream(path,                 append));         (superobject o : this.list) {             if (o instanceof object1) {                 ((subobject1) w).writer1(file);             }             if (o instanceof object2) {                 ((subobject3) w).writer2(file);              }if (o instanceof object3) {                 ((subobject3) w).writer3(file);              }         }         write.close();     } catch (filenotfoundexception e) {         // todo auto-generated catch block         e.printstacktrace();     } } 

in object's writer method try first print line says type , call writer method innerobject. after want current object's arguments printed , goes lists writer method

public void writer1(string file) {     file path = new file("../opdracht6_2/src/" + file);     try {         printwriter write = new printwriter(                 new fileoutputstream(path, true));         //below string want print before innerobject appends          //its own arguments file         write.append("string\r\n");         this.innerobject.innerwriter();         write.append(this objects arg);         write.close();     } catch (ioexception e) {         // todo auto-generated catch block         e.printstacktrace();     } } 

the innerobject's writer

public void innerwriter(string file) {     file path = new file("../opdracht6_2/src/" + file);     try {         printwriter write = new printwriter(                 new fileoutputstream(path, true));         write.append(this objects arg);         write.close();     } catch (ioexception e) {         // todo auto-generated catch block         e.printstacktrace();     } } 

now thing happening line i'm trying append first gets appended after innerobject's arguments though have put before method calls innerobject's writer. looks in file:

inner objects arg

string

outer objects arg

can explain why?

it's better not use writer in each method here. use single stringbuilder append content , pass through methods. writer not flushing contents in order in content appended. specifically, statement write.close() inside innerwriter flush contents of inner object before "string\r\n" written writer in caller method.

you can avoid creating multiple writers , use stringbuilder instead:

// pass stringbuilder append public void innerwriter(stringbuilder sb) {     sb.append(this objects arg); } 

and when you're done appending content, write using writer created once:

printwriter write = new printwriter(new fileoutputstream(path,             append)); stringbuilder sb = new stringbuilder(); (superobject o : this.list) {     if (o instanceof object1) {         ((subobject1) w).writer1(sb);     }     if (o instanceof object2) {         ((subobject3) w).writer2(sb);      } if (o instanceof object3) {         ((subobject3) w).writer3(sb);      } }  write.append(sb.tostring()); write.close(); 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -