您的方法确实有效,只是忘记刷新输出:
try { BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); log.write("This will be printed on stdout!n"); log.flush();}catch (Exception e) { e.printStackTrace();}
两者
OutputStreamWriter和
PrintWriter都是
Writer实例,因此您可以执行以下 *** 作:
BufferedWriter log;Writer openForFile(String fileName) { if (fileName != null) return new PrintWriter(fileName); else return new OutputStreamWriter(System.out);}log = new BufferedWriter(openForFile(null)); //stdoutlog = new BufferedWriter(openForFile("mylog.log")); // using a file
或任何东西,只是为了给您这个主意。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)