java - new PrintWriter(new BufferedWriter(new FileWriter("output.txt", true))) is not printing -
i want write output.txt without clearing - appending end. however, when use following 2 methods:
public void addemails(arraylist<string> emails){ (int = 0; < emails.size(); i++){ system.out.println(emails.get(i)); writer.println(emails.get(i)); if (i == emails.size() - 1){ system.out.println(); system.out.println(); writer.println(); writer.println(); } } } public void addfilename(string fn){ string filename = fn.replace("%date%.%format%", ""); writer.println(filename); writer.println(); system.out.println(filename); system.out.println(); }
these called following methods:
public void analyze(string path){ try { system.out.println(path); file inputfile = new file(path); inputstream inputstream= new fileinputstream(inputfile); reader reader = new inputstreamreader(inputstream,"utf-8"); inputsource = new inputsource(reader); is.setencoding("utf-8"); saxparserfactory factory = saxparserfactory.newinstance(); saxparser saxparser = factory.newsaxparser(); descriptorhandler deschandler = new descriptorhandler(this); saxparser.parse(is, deschandler); reader.close(); writer.close(); } catch (exception e) { e.printstacktrace(); } }
saxparser.parse uses following overloaded methods defaulthandler:
@override public void startelement(string uri, string localname, string qname, attributes attributes) throws saxexception { // if (qname.equalsignorecase("student")) { // string rollno = attributes.getvalue("rollno"); // system.out.println("roll no : " + rollno); if (qname.equalsignorecase("destinationgroup")) { bdestinationgroup = true; } else if (qname.equalsignorecase("emaildestination")) { bemaildestination = true; } else if (qname.equalsignorecase("filename")) { bfilename = true; } } @override public void endelement(string uri, string localname, string qname) throws saxexception { if (qname.equalsignorecase("destinationgroup")) { descriptoranalyzer.addfilename(filename); descriptoranalyzer.addemails(emails); emails.clear(); bdestinationgroup = false; } } @override public void characters(char ch[], int start, int length) throws saxexception { if (bemaildestination) { string s = new string(ch, start, length); emails.add(s); bemaildestination = false; } else if (bfilename) { filename = new string(ch, start, length); bfilename = false; } }
the declaration (called on program start-up):
writer = new printwriter(new bufferedwriter(new filewriter("output.txt", true)));
system.out.println() doing want. however, output.txt remains unchanged.
edit: writes file, instead of appending it, clears , writes again.
edit again: misunderstood. after more testing, writes file on first call of analyze(). on subsequent calls, nothing.
edit 3: solved problem using flush() instead of close(), printwriter not close. believe printwriter closing caused failure on subsequent writes.
bufferedwritter doesn't write data file every time use println. whole point of save in buffer , when buffer full write. problems happen when never reach buffer maximum size , dont call flush on writer. it's same bufferedoutputstream.
so should call writer.flush() before closing it.
Comments
Post a Comment