代码示例:
//打印流:字节流:printStream 字符流:printWriter @Test public void printStreamWriter() { FileOutputStream fos = null; try { fos = new FileOutputStream(new File("print.txt")); } catch (FileNotFoundException e) { e.printStackTrace(); } PrintStream ps = new PrintStream(fos, true); System.setOut(ps); //把标准输出流(控制台输出)改成文件 for (int i = 0; i < 255; i++) { //输出ASCII字符 System.out.print((char) i); if (i % 50 == 0) { System.out.println(); } } ps.close(); }
本文介绍了一个Java示例程序,该程序展示了如何使用PrintStream类将标准输出重定向到文件。通过创建FileOutputStream并将PrintStream实例化为该输出流,可以实现将原本输出到控制台的内容改写入指定文件的功能。此示例还演示了如何输出ASCII字符集。
875

被折叠的 条评论
为什么被折叠?



