我有一个java swing应用程序,我试图让用户能够在文件到控制台之间来回切换stdout。这是一个切换。我可以切换文件,但无法切换回控制台。当我切换回来时,我也没有得到任何输出。
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
}
编辑:
我尝试过找到here的解决方案:
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
和here。
这些似乎都不起作用。