java _io_打印流,PrintStream和PrintWriter

*PrintStream和PrintWriter,效果相似

  • PrintStream ps=new PrintStream(字节流,true/false),为真则自动刷新内容,默认为假
  • System.out默认为PrintStream 打印流
  • 写入文件(通过.println):
  • PrintStream ps=new printStream(new BufferedStream(FileOutputStream("vv.txt")),true)
  • ps.println("ad")
  • 重定向输出端(输出到文件):System.setOut(ps); ps.println("aa");
  • 重定向回控制台:System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)),true));

PrintStream:

        //打印流System.out
        PrintStream os =System.out;
        os.println("haha");

    //写入文件
    os= new PrintStream(new BufferedOutputStream(new FileOutputStream("vv.txt")),true);
    os.println("add");
    os.println(false);
    os.flush();
    os.close()

    //重定向输出端(输出到文件)
    System.setOut(os);
    System.out.println("change");

    //重定向回控制台
    System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)),true));
    //标准的输入输出端

PrintWriter:

    PrintWriter pw=new PrintWriter(new BufferedOutputStream(new FileOutputStream("vv.txt")),true);
    pw.println("ff");
    pw.println(true);
    pw.close();

猜你喜欢

转载自blog.51cto.com/14437184/2425419