try { PrintStream out = new PrintStream(fileName); System.setOut(out); } catch(FileNotFoundException e) { e.printStackTrace(); } 设置后System.out.println()将会把打印的内容直接输出到文件fileName中去。