Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6079185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:55:05+00:00 2026-05-23T10:55:05+00:00

So I was wondering if it was possible to write all the console output

  • 0

So I was wondering if it was possible to write all the console output to a separate file outside of Java? I know about the Printwriter and Filewriter method. However, in my experience those would work if I was using them all within one method, but I don’t think I can do that with the code I have right now. Below is what I 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 and 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 the qName is "w:p" and it is in the heading, print out the heading and then reset
            if ("w:p".equals(qName) && inHeading == true)
            {       
                System.out.println(heading);        

                heading = "";
                inHeading = false;      
            }   
        }

        // method to verify whether element is an 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 all files in directory
        File[] files = directory.listFiles();
        for (File file : files)
        {
            if (file.getName().equals("document.xml"))
            {
                try 
                {
                    // creates and returns new instance of SAX-implementation:
                    SAXParserFactory factory = SAXParserFactory.newInstance();

                    // create SAX-parser...
                    SAXParser parser = factory.newSAXParser();

                    // prints out the current working proposal, traversing up the directory structure
                    System.out.println(file.getParentFile().getParentFile().getName());

                    // .. define our handler:
                    SaxHandler handler = new SaxHandler();

                    // and parse:
                    parser.parse(file.getAbsolutePath(), handler);

                    try 
                    {
                        // instantiates new printwriter which writes out to a 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 a directory so (recursively) traverse it
                traverse(file);
            }
        }
    }
}

So I’ve instantiated the printwriter in there, but obviously it’s no good if I have nothing to write to it. So I’m not really sure how I can get what’s printing out the console to be written to that file. Any ideas? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:55:05+00:00Added an answer on May 23, 2026 at 10:55 am

    If you really want to you can redirect System.out to any PrintStream like this:

    PrintStream stream = new PrintStream("filename.txt");
    System.setOut(stream);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As you all know using java it is possible to create methods which require
Possible Duplicate: Why is Java's Iterator not an Iterable? We all know java's extended
I was sitting here wondering if it's possible to write a greasemonkey script that
I was wondering if it is possible for C# to write objects to the
I was wondering if i could possible write an app, that could be a
I was wondering if it's possible to write something like this: <Window ... xmlns
I was wondering if it is possible to write both the dictionary key and
I was wondering if it was possible to tell bash that all calls to
I am wondering if it's possible to write an application that will access a
I'm wondering if only by applying some standard algorithms is possible to write a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.