Java IO流(PrintWriter)

打印流:该流提供了打印方法,可以将各种数据类型的数据都原样打印。

字节打印流:
PrintStream
构造函数可以接收的参数类型:

  • 1.file对象。
  • 2.字符串路径。String
  • 3.字节输出流。OutputStream

字符打印流
PrintWriter
构造函数可以接收的参数类型:

  • 1.file对象。File
  • 2.字符串路径。String
  • 3.字节输出流。OutputStream
  • 4.字符输出流。Writer
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class PrintStreamDemo {
    public static void main(String[] args) throws IOException
    {
        BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);
        String line = null;
        while ((line = bufr.readLine())!=null)
        {
            if ("over".equals(line))
                break;
            out.println(line.toUpperCase());
            out.flush();
        }
        out.close();
        bufr.close();
    }
}
Java IO流(PrintWriter)_第1张图片
2.gif

自动刷新

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class PrintStreamDemo {
    public static void main(String[] args) throws IOException
    {
        BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out,true);
        String line = null;
        while ((line = bufr.readLine())!=null)
        {
            if ("over".equals(line))
                break;
            out.println(line.toUpperCase());
        }
        out.close();
        bufr.close();
    }
}
Java IO流(PrintWriter)_第2张图片
2.gif

//输出到文件中

import java.io.*;
public class PrintStreamDemo {
    public static void main(String[] args) throws IOException
    {
        BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("a.txt",true)));
        String line = null;
        while ((line = bufr.readLine())!=null)
        {
            if ("over".equals(line))
                break;
            out.println(line.toUpperCase());
        }
        out.close();
        bufr.close();
    }
}
Java IO流(PrintWriter)_第3张图片
2.gif

Java IO流(PrintWriter)_第4张图片
Snip20190928_2.png

你可能感兴趣的:(Java IO流(PrintWriter))