Detailed explanation of several methods of formatted output in print stream PrintStream&Java

Detailed printing stream PrintStream

java.io.PrintStream: Print Stream
PrintStream: Adds functionality to other output streams, enabling them to conveniently print various data value representations.
1.1 PrintStream features:

  • 1. Only responsible for data output, not for data reading
  • 2. Unlike other output streams, PrintStream never throws IOException
  • 3. Unique method: print, println
    void print (any type of value)
    void println (any type of value and newline)

1.2 Construction method:

  • PrintStream(File file): The output destination is a file
  • PrintStream(OutputStream out): The output destination is a byte output stream
  • PrintStream(String fileName): The output destination is a file path


1.3 The member method PrintStream extends OutputStream inherited from the parent class

  • public void close() : Closes this output stream and frees any system resources associated with this stream.
  • public void flush() : Flushes this output stream and forces any buffered output bytes to be written out.
  • public void write(byte[] b ) : Writes b.length bytes from the specified byte array to this output stream.
  • public void write(byte[] b,int off,int len)
    : Writes len bytes from the specified byte array, starting from offset off to this output stream.
  • public abstract void write(int b) : Outputs the specified bytes to the stream.

1.4 Note:

  • If you use the write method inherited from the parent class to write data, then you will query the encoding table 97–>a when viewing the data
  • If you use your own unique method print/println method to write data, the written data will output 97–>97 as it is
package com.IOAndProperties.PrintStream;

import java.io.FileNotFoundException;
import java.io.PrintStream;

	/*
    java.io.PrintStream:打印流
        PrintStream:为其他输出流添加了功能,使他们能够方便地打印各种数据值表示形式。
     PrintStream特点:
        1.只负责数据的输出,不负责数据的读取
        2.与其他输出流不同,PrintStream永远不会抛出IOException
        3.特有的方法:print,println
              void print(任意类型的值)
              void println(任意类型的值并换行)

     构造方法:
         PrintStream(File file):输出的目的地是一个文件
         PrintStream(OutputStream out):输出的目的地是一个字节输出流
         PrintStream(String fileName):输出的目的地是一个文件路径

     PrintStream extends OutputStream
     继承自父类的成员方法
         -public void close() :关闭此输出流并释放与此流相关联的任何系统资源。
         -public void flush() :刷新此输出流并强制任何缓冲的输出字节被写出。
         -public void write(byte[] b ) :将b.length字节从指定的字节数组写入此输出流。
         -public void write(byte[] b,int off,int len) :从指定的字节数组写入len字节,从偏移量off开始输出到此输出流。
         -public abstract void write(int b) :将指定的字节输出流。

      注意:
           如果使用继承自父类的write方法写数据,那么查看数据的时候会查询编码表 97-->a
           如果使用自己特有的方法print/println方法写数据,写的数据原样输出   97-->97
 */
public class Demo01PrintStream {
    
    
    public static void main(String[] args) throws FileNotFoundException {
    
    
//        System.out.println("HelloWorld");

        //创建打印流PrintStream对象,构造方法中绑定要输出的目的地
        PrintStream ps = new PrintStream("基础语法\\print.txt");
        //  如果使用继承自父类的write方法写数据,那么查看数据的时候会查询编码表 97-->a
        ps.write(97);   //a

        ps.println(97);
        ps.println(8.7);
        ps.println("adc");
        ps.println(true);
        ps.println('c');

        //释放资源
        ps.close();
    }
}

1.5 You can change the destination of the output statement (the flow direction of the print stream)

  • Output statement, output in the console by default
  • Use the System.setOut method to change the destination of the output statement to the destination of the print stream passed by the parameter
  • static void setOut(PrintStream out)
    Reassigns the "standard" output stream.
package com.IOAndProperties.PrintStream;

import java.io.FileNotFoundException;
import java.io.PrintStream;

/*
   可以改变输出语句目的地(打印流的流向)
   输出语句,默认在控制台输出
   使用 System.setOut方法改变输出语句的目的地改为参数传递的打印流的目的地
        static void setOut(PrintStream out)
        重新分配“标准”输出流。
 */
public class Demo02PrintStream {
    
    
    public static void main(String[] args) throws FileNotFoundException {
    
    
        System.out.println("我是在控制台输出");

        PrintStream ps = new PrintStream("基础语法\\目的地是打印流.txt");
        System.setOut(ps);  //把输出语句的目的地改变为打印流的目的地
        System.out.println("我在打印流的目的地中输出");

        ps.close();
    }
}

1.6 output result
insert image description here

Several ways to achieve formatted output in Java:

1System.out.printf();

类似于c语言的printf方法。如:
int x = 55;
System.out.format(“x = %5x”, x);
输出结果为:x = 37

2System.out.format()

int x = 55;
System.out.printf(“x = %5c”, x);
输出结果为:x = 7

3String.format()

String提供的静态方法。
int x = 55;
System.out.println(String.format(“x = %5x”, x));
输出结果为:x = 37

4Formatter

int x = 55;
Formatter format = new Formatter(System.out);
format.format(“x = %5x”, x);
输出结果为:x = 37

Guess you like

Origin blog.csdn.net/qq_43842093/article/details/130093242