0

Code for redirecting the print output within the program:

   try
   {
       System.out.println("Print on console");

       // Store console print stream.
       PrintStream ps_console = System.out;

       File file = new File("C:/Users/John/Desktop/compiletest/output" + trialNum + ".txt");
       FileOutputStream fos = new FileOutputStream(file);
       PrintStream ps = new PrintStream(fos);
       System.setOut(ps);
       System.out.println("Print in the file !!");

       // Set console print stream.
       //System.setOut(ps_console);
       System.out.println("Console again !!");
    }
    catch(Exception e)
    {
        System.out.println("Mission failed, we'll get em next time");
    }

This method works fine if it is implemented in the program I want to redirect its output. In my case I want a separate class that will redirect the output of a certain program to a txt file.

If there is a simpler way to complete this task I am greatly inclined to hear.

  • How are you running the separate program? – user253751 Jun 06 '18 at 05:12
  • 3
    Understandably, you may have a reason to change output in your program like this, but this isn't "best practice". There are ways of capturing output from a program and placing it in files. If you force your hand on the matter, it is no longer flexible (you couldn't, say, prevent it from writing to the file without recompiling the entire program). – Neil Jun 06 '18 at 06:49
  • 1
    If this is not intended to be a console program, use a logging mechanism such as log4j instead of printing to `System.out`. – Hulk Jun 06 '18 at 07:33
  • `a separate class that will redirect the output of a certain program to a txt file` - a class cannot guide another to redirect console output, until the code written accepts the same(such as accepting the outputfile as execution arguments and act accordingly). Since this is a System property, this can only be set before execution starts. So, in your case, if code of the class-whose-output-needs-to-be-captured cannot be changed, there is no hope. – soufrk Jun 06 '18 at 08:10
  • This may be a duplicate of https://stackoverflow.com/questions/882772/capturing-stdout-when-calling-runtime-exec if you are able/want to run it as a separate process. – Ryan Leach Jun 06 '18 at 08:13
  • Why is this a stackoverflow question and not a softwareengineering one? –  Jun 07 '18 at 02:29

3 Answers3

1

This seems like a perfect usage example for a logging framework.

There are many other libraries you can use. In general it is always preferable to use a separate stream for files - if you must redirect System.out it could be useful to keep the original PrintStream before you change it so that you can still write to the console if you need to:

PrintStream ORIGINAL_SOUT = System.out;
System.setOut(ps);

System.out.println("ABC"); // Uses file
ORIGINAL_SOUT.println("ABC"); // Uses console

A simple usage example of a logging framework (from the first linked library):

private static final Logger logger = LoggerFactory.getLogger(YourClass.class);

logger.debug("A message at DEBUG level");
logger.info("A message at INFO level");
logger.warn("A message at WARN level");
logger.error("A message at ERROR level");
Paul Benn
  • 1,911
  • 11
  • 26
  • "redirecting System.out is a change you would have to revert every time you want output in your console." Could you not clone the stream instead, and still write it to the original console? – Ryan Leach Jun 06 '18 at 08:16
  • You mean by setting System.out to an extra PrintStream variable? Yes this is correct, I'll edit the question to reflect that. My mistake. – Paul Benn Jun 06 '18 at 08:24
0

I think you should create Temporary function for holding and storing your data with temporary and then execute your transactions anyway you want. Finally you can store your data wherever you want with permanently. This is the most known method I know. You should check the link for understanding easier.

http://www.daveoncsharp.com/2009/09/how-to-use-temporary-files-in-csharp/

user5695111
  • 111
  • 2
  • 13
0

Logging software (such as SLF4J,Log4J etc)is better option. It has different ways to create file,different file formats, rolling appenders(based on dates/sequence/size), logs based on priorities

TheSprinter
  • 1,523
  • 17
  • 30