title: Java redirect stdout to file and back to console
body: <p>I have a java swing application that I am trying to give the user the ability to switch stdout back and forth from file to console.  This is meant to be a toggle.  I can toggle on file, but am not able to toggle back to console.  When I toggle back, I do not get any output on either.</p>
  
  <pre><code>    private void btnPrefsConsoleActionPerformed(java.awt.event.ActionEvent evt)                                                
      {                                                    
          outputTo = "console";                                   // Set output to console
  
  ***** FIX ***** comment next 2 lines and insert 3rd
          pStream = System.out;
          System.setOut(pStream);                                 // Set output back to console
      System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
  ***** FIX *****
          btnPrefsConsole.setSelected(true);                      // Turn on console
          btnPrefsSysout.setSelected(false);                      // Turn off sysout
          monitorPrefs.put(MONITOROUTPUT, outputTo);              // Set in prefs
      }                                               
  
      private void btnPrefsSysoutActionPerformed(java.awt.event.ActionEvent evt)                                               
      {                                                   
          outputTo = "sysout";                                    // Set output to console
          btnPrefsConsole.setSelected(false);                     // Turn off console
          btnPrefsSysout.setSelected(true);                       // Turn on sysout
  
  //              Switch System.out to file 
          try
          {
              pStream = new PrintStream(new BufferedOutputStream(
                      new FileOutputStream(dbPath + "/sysout.txt")),true);
              System.setOut(pStream);
          }
          catch (FileNotFoundException ex)
          {
              Logger.getLogger(DRHandicapMonitor.class.getName()).log(Level.SEVERE, null, ex);
          }
  
          monitorPrefs.put(MONITOROUTPUT, outputTo);              // Set in prefs
      }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>I have tried solutions found <a href="https://stackoverflow.com/questions/5339499/resetting-standard-output-stream?noredirect=1&amp;lq=1">here</a>:</p>
  
  <pre><code>System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
  </code></pre>
  
  <p>and <a href="https://stackoverflow.com/questions/3340342/java-writing-to-the-real-stdout-after-system-setout">here</a>.</p>
  
  <p>None of which seemed to work.</p>