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

时间:2020-04-24 18:42:28   收藏:0   阅读:46
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

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!