IO stream - print stream

Overview:

In the entire IO package, print stream is a convenient class for outputting information, mainly including byte print stream (PrintStream) and character print stream (PrintWriter). The print stream provides a very convenient printing function that can print any data type, such as decimals, integers, strings, Boolean types, etc.

1. PrintStream class

 Why have PrintStream class?

In the past, OutputStream was needed to print information, but in this way, it would be very troublesome to output all the data, String——>byte[], but the output can be conveniently done in the byte print stream.

Advantages of PrintStream class

PrintStream provides enhanced functions based on OutputStream, which can conveniently output formatted representations of various types of data (not limited to byte types).

1.1 Construction method

0f4720c58a664216b4c7151cd74af145.png

 In the constructor defined in PrintStream, it can be clearly found that there is a constructor that can directly receive an instance of the OutputStream class. This is because compared to OutputStream, PrintStream can output data more conveniently, which is like repackaging OutputStream. , making the output more convenient.

1.2 Common methods

This method is overloaded many times and can output arbitrary data.

public void print(Type x);

This method has been overloaded many times and can output arbitrary data and then wrap.

public void println(Type x);

1.3 Write data to the file

[Example] Write the specified data to the file

public class PrintStreamDemo {

   public static void main(String[] args) throws FileNotFoundException {

      //Create a character printing stream object

      PrintStream ps = new PrintStream("demo.txt");

      //Input the element values ​​​​in the array into the file

      Object[] arr = { true, 123, "powernode", 123.45};

      for(int i = 0; i < arr.length; i++) {

         // print data

         ps.println(arr[i]);

      }

      ps.close();

   }

}

1.4 Formatted output

After JDK1.5, JAVA expanded the PrintStream class and added a formatted output method. You can directly use the printf() method to complete the operation, but you need to specify the output data type when formatting output.

character

describe

%s

Represents the content as a string

%d

Represents the content as an integer

%f

Indicates that the content is a decimal

%c

Represents content as characters

[Example] Formatted output case

public class PrintStreamDemo {

   public static void main(String[] args) throws FileNotFoundException {

      //Create a character printing stream object

      PrintStream ps = new PrintStream("demo.txt");

      //Format output

      ps.printf("姓名:%s 年龄:%d 成绩:%f 性别:%c","小明", 8, 97.5, '男');

      ps.close();

      // System.out的格式化输出

      System.out.printf("姓名:%s 年龄:%d 成绩:%f 性别:%c", "小明", 18, 97.5, '男');

   }

}

 二、PrintWriter类

概述:

PrintWriter提供了PrintStream的所有打印方法,其方法也从不抛出IOException。

PrintWriter与PrintStream的区别:

作为处理流使用时,PrintStream只能封装OutputStream类型的字节流,而PrintWriter既可以封装OutputStream类型的字节流,还能够封装Writer类型的字符输出流并增强其功能。

1.1 构造方法

aa8004063fe1493d9e1658cfb657d616.png

注意:设置了自动刷新,那么只有在换行的时候才会提现效果!

1.2 把数据写入到文件中

【示例】把指定的数据,写入到文件中

public class PrintWriterDemo {

   public static void main(String[] args) throws IOException {

      FileWriter writer = new FileWriter("demo.txt");

      // 创建字符打印流对象,并设置为自定刷新

      PrintWriter pw = new PrintWriter(writer, true);

      // 存储数据

      for(int i = 0; i < 10; i++) {

         pw.println("hello world");

      }

      pw.close();

   }

}

补充:字符打印流也支持数据的格式化输出。

 三、标准输入&输出流

概述:

Java通过系统类System实现标准输入&输出的功能,定义了3个流变量:in、out和err。这三个流在Java中都定义为静态变量,可以直接通过System类进行调用。

1)System.in表示标准输入,通常指从键盘输入数据;

2)System.out表示标准输出,通常指把数据输出到控制台或者屏幕;

3)System.err表示标准错误输出,通常指把数据输出到控制台或者屏幕。

1.1 简单标准输入(System.in)

System.in作为字节输入流类InputStream的对象实现标准输入,通过read()方法从键盘接受数据。 

【示例】获取控制台输入的数据

public class InputStreamDemo {

   public static void main(String[] args) throws IOException {

      // 获取一个标准的输入流

      InputStream in = System.in;

      // 读取键盘输入的内容

      byte[] by = new byte[1024];

      int count = in.read(by);

      // 遍历输出读取到的内容

      for(int i = 0; i < count; i++) {

            System.out.println("-->" + by[i]);

      }

      // 注意:通过System.in获取到的标准输入流不用关闭!

   }

}

运行程序,从键盘输入3个字符abc并按Enter键,保存在缓冲区bf中的元素个数count为5,Enter占用最后两个字节('\r'和'\n'),下图为控制台的效果。

e440ee3fd54c464695c8ed6a22010e36.png

在通常情况下需要从标准输入读取字符,整数,浮点数等具体类型的数据System.in作为标准输入流,是InputStream类的一个对象,其read()方法的主要功能是读取字节和字节数组,不能直接得到需要的数据(如整型,浮点型)。此时,需要另外一个类java.util.Scanner的配合。可以利用Scanner类对标准输入流System.in的数据进行解析,得到需要的数据。

1.2 简单标准输出(System.out) 

System.out作为打印流PrintStream的对象实现标准输出,其定义了print和println方法,支持将Java的任意基本类型作为参数。 

【示例】在控制台输输出的数据

public class PrintStreamTest {

    public static void main(String[] args) {

        // 获取一个标准的输出流

        PrintStream ps = System.out;

        // 在控制台输出数据

        ps.println(123);

        ps.println(123.456);

        ps.println("hello world");

        ps.println(true);

        ps.println('A');

        ps.println(new Date());

      // 注意:通过System.out获取到的标准输出流不用关闭!

    }

}

Note: It is recommended not to close the standard input & output streams obtained through System.in and System.out!

 

 

Guess you like

Origin blog.csdn.net/shengshanlaolin_/article/details/127458488