title: Redirecting FileOutputStream to console - Java
body: <p>I'd like to rewrite and simply my code to cut down on the number of methods in a class that do exactly the same thing but either write to a file, or to a console so I can do things like:</p>
  
  <pre><code>PrintFlightSchedule(String aFileName); // prints to a file
  PrintFlightSchedule(); // writes to console.
  </code></pre>
  
  <p>I've tried creating the following test method just to demonstrate what I'my trying to achieve, by defining an abstract OutputStream, then instantiating it as either a PrintStream, or console (via System.out):</p>
  
  <pre><code>public static void testOutputStream(String fileNm, String msg) {
      OutputStream os;
      if (fileNm.equals("") ) { // No file name provided, write to console
          os = System.out;
      }
      // File name provided, write to this file name
      else {
          try {
              os = new FileOutputStream(fileNm);
          }
          catch (FileNotFoundException fe) {
              System.out.println("File not found " + fe.toString());
          }
      }
      // Use the output stream here - ideally println method?
      // os.println or write(6);
  }
  </code></pre>
  
  <p>This is admittedly half-assed, but it gives you an idea what I'd like to achieve.</p>
  
  <p>Is there a way in Java to define the output method (file or console) at run-time, so I can use the same methods to do either, at runtime? I guess a simple way would be to redirect the FileOutputStream to the console - is that possible?</p>