sax - Writing Console Output to File in Java -
so wondering if possible write console output separate file outside of java? know printwriter , filewriter method. however, in experience work if using them within 1 method, don't think can code have right now. below have...
java code
import java.io.bufferedwriter; import java.io.file; import java.io.filewriter; import java.io.ioexception; import java.io.printwriter; import org.xml.sax.*; import org.xml.sax.helpers.defaulthandler; import javax.xml.parsers.saxparser; import javax.xml.parsers.saxparserfactory; public class xmltagparser extends defaulthandler { private int i; public xmltagparser() { traverse(new file("c:/documents , settings/user/workspace/intern project/proposals/converted proposals/extracted items")); } private static final class saxhandler extends defaulthandler { private stringbuffer buffer; private string heading; private boolean inheading; public void startelement(string uri, string localname, string qname, attributes attrs) { if ("w:pstyle".equals(qname)) { string val = attrs.getvalue("w:val"); if (val.contains("heading")) { if (isheading(val)) { system.out.println(val); inheading = true; } } } if("w:t".equals(qname)) { if (inheading == true) { buffer = new stringbuffer(); } } } public void characters(char buff[], int offset, int length) throws saxexception { string s = new string(buff, offset, length); if(buffer != null) { buffer.append(s); heading = heading += s; } } public void endelement(string uri, string localname, string qname) { buffer = null; //if qname "w:p" , in heading, print out heading , reset if ("w:p".equals(qname) && inheading == true) { system.out.println(heading); heading = ""; inheading = false; } } // method verify whether element actual heading private static boolean isheading(string heading) { string headingnumber = heading.substring(7,8); string headingname = heading.substring(0,7); if (headingname.equals("heading")) { if (headingnumber.equals("1") || headingnumber.equals("2") || headingnumber.equals("3") || headingnumber.equals("4") || headingnumber.equals("5") || headingnumber.equals("6")) { return true; } } return false; } } /*private void writefile(file file) { try { printwriter out = new printwriter(new filewriter(file + "/" + i++)); out.close(); } catch (ioexception e) { e.printstacktrace(system.out); } }*/ private void traverse(file directory) { //get files in directory file[] files = directory.listfiles(); (file file : files) { if (file.getname().equals("document.xml")) { try { // creates , returns new instance of sax-implementation: saxparserfactory factory = saxparserfactory.newinstance(); // create sax-parser... saxparser parser = factory.newsaxparser(); // prints out current working proposal, traversing directory structure system.out.println(file.getparentfile().getparentfile().getname()); // .. define our handler: saxhandler handler = new saxhandler(); // , parse: parser.parse(file.getabsolutepath(), handler); try { // instantiates new printwriter writes out file printwriter out = new printwriter(new filewriter(file.getparentfile().getparentfile() + "/" + i++ + ".txt")); out.close(); } catch (ioexception e) { e.printstacktrace(system.out); } } catch (exception ex) { ex.printstacktrace(system.out); } } else if (file.isdirectory()) { //it's directory (recursively) traverse traverse(file); } } } }
so i've instantiated printwriter in there, it's no if have nothing write it. i'm not sure how can what's printing out console written file. ideas? in advance.
if want can redirect system.out
printstream
this:
printstream stream = new printstream("filename.txt"); system.setout(stream);
Comments
Post a Comment