java _io_ print stream, PrintStream and PrintWriter

* PrintStream and PrintWriter, similar effects

  • PrintStream ps = new PrintStream (byte stream, true / false), automatically refresh the content is true, the default is false
  • The default is System.out PrintStream print streams
  • Write to the file (by .println):
  • PrintStream ps=new printStream(new BufferedStream(FileOutputStream("vv.txt")),true)
  • ps.println("ad")
  • Redirect the output terminal (output to a file): System.setOut (ps); ps.println ( "aa");
  • Redirected back to the console: 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();

Guess you like

Origin blog.51cto.com/14437184/2425419