Java IO stream - detailed use of print stream

print stream

insert image description here

Basic use of print streams

print stream :

Function: The print stream can realize more convenient and efficient printing of data to files . Print stream generally refers to two classes: PrintStream and PrintWriter.

It can be realized that whatever data is printed is what data , for example, printing the integer 97 is 97, and printing boolean true is true.

The print stream PrintStream constructor is as follows :

constructor illustrate
PrintStream(OutputStream os) The print stream goes directly to the byte output stream pipe
PrintStream(File f) The print stream goes directly to the file object
PrintStream(String filepath) The print stream goes directly to the file path

sample code

public static void main(String[] args) throws Exception {
    
    
    // 方式一: 打印流通向字节输出流管道
    PrintStream ps1 = new PrintStream(new FileOutputStream("/Users/chenyq/Documents/test.txt"));

    // 方式二: 打印流通向文件对象
    PrintStream ps2 = new PrintStream(new File("/Users/chenyq/Documents/test.txt"));

    // 方式三: 打印流通向文件路径
    PrintStream ps3 = new PrintStream("/Users/chenyq/Documents/test.txt");
}

The print stream PrintStream method is as follows :

method illustrate
print(Xxx xx) Print any type of data out (without newline)
println(Xxx xx) Print any type of data out (newline)

demo code

public static void main(String[] args) throws Exception {
    
    
    PrintStream ps = new PrintStream(new FileOutputStream("/Users/chenyq/Documents/test.txt"));

    // 打印流方法
    ps.println(97);
    ps.println("aaa");
    ps.println(123);
    ps.println('我');
    ps.println(true);

    // 关闭流
    ps.close();
}

What the above code prints to the file is as follows:

insert image description here

The print stream PrintWrite constructor is as follows

constructor illustrate
PrintWriter(OutputStream os) The print stream goes directly to the byte output stream pipe
PrintWriter (Writer w) The print stream goes directly to the character output stream pipe
PrintWriter (File f) The print stream goes directly to the file object
PrintWriter (String filepath) The print stream goes directly to the file path

Print stream PrintStream method :

method illustrate
print(Xxx xx) Print any type of data out (newline)
println(Xxx xx) Print any type of data out (without newline)

Demo code :

There is no difference between PrintWrite and PrintPrintStream in the use of printing

public static void main(String[] args) throws Exception {
    
    
    PrintWriter pw = new PrintWriter(new FileOutputStream("/Users/chenyq/Documents/test.txt"));

    // 打印流方法
    pw.println(97);
    pw.println("aaa");
    pw.println(123);
    pw.println('我');
    pw.println(false);

    // 关闭流
    pw.close();
}

Since there is no difference between PrintStream and PrintWrite in printing, what is the difference between these two classes ?

The print data functions are exactly the same, all of which are easy to use and efficient in performance (core advantages)

PrintStream inherits from byte output stream OutputStream and supports the method of writing byte data .

PrintWriter inherits from character output stream Writer and supports writing character data out.

public static void main(String[] args) throws Exception {
    
    
    PrintStream ps = new PrintStream(new FileOutputStream("/Users/chenyq/Documents/test.txt"));

    // PrintStream写字节数据
    ps.write("我爱学习".getBytes());
}
public static void main(String[] args) throws Exception {
    
    
    PrintWriter pw = new PrintWriter(new FileOutputStream("/Users/chenyq/Documents/test.txt"));

    // PrintWrite写字符数据
    pw.write("我爱学习");
}

But we generally use the print stream to print data to the file;

We basically don't use print streams to write data, because it's better to use character output streams and byte output streams directly to write data

Using the small details of the print stream, we can find that the print stream also covers the pipeline by default, and the printed data will overwrite the original data

If we want to print the data as additional data, we need to enable the append mode in the low-level pipeline, and the constructor of the print stream does not provide additional data too early

public static void main(String[] args) throws Exception {
    
    
    // 在原始字符输出流开启追加模式
    PrintWriter pw = new PrintWriter(new FileOutputStream("/Users/chenyq/Documents/test.txt", true));
}

output statement redirection

Output statement redirection ( understand )

An application belonging to the print stream, which can change System.out.println();the printing position of the output statement to the file.

For example: In the development stage of the project, we print and debug on the console, and we hope that the output statement will be printed to the file after the project goes online

public static void main(String[] args) throws Exception {
    
    
    PrintStream ps = new PrintStream("/Users/chenyq/Documents/test.txt");

    // 改变输出语句的位置(重定向), 将输出语句输出到文件当中
    System.setOut(ps);
    System.out.println("aaa");
    System.out.println(97);
    System.out.println(123);
    System.out.println('我');
}

Guess you like

Origin blog.csdn.net/m0_71485750/article/details/127642540