改变输出流流向示例(printStream)

import java.io.*;

/**
* @auto dh
* @create 2020-04-24-16:24
*/
public class File005 {
public static void main(String[] args) {
BufferedOutputStream br = null;
try {
br = new BufferedOutputStream(new FileOutputStream("D:" + File.separator + "abc.txt"));
PrintStream ps = new PrintStream(br, true);
//改变输出流的流向
if (ps != null) {
System.setOut(ps);
}
for(int i=0;i<=255;i++){
System.out.print(i+" ");
if(i>0 &&i%50==0){
System.out.println();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
if(br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
原文地址:https://www.cnblogs.com/kukai/p/12768390.html