(Byte stream and character stream) OutputStream byte output stream

Byte output stream: OutputStream

Byte data is based on the byte type as the main operation. You can use OutPutStream to complete the byte content output. The basic definition of this class is as follows:

public abstract class OutputStream extends Object implements Closeable, Flushable

First of all, you can find that this class implements two interfaces, so the basic correspondence is as follows:

The OutputStream class defines a common output operation standard, and a total of three content output methods are defined in this operation standard

  • Output a single byte of data: public abstract void write​(int b) throws IOException
  • Output a set of byte data: public void write​(byte[] b) throws IOException
  • Coarse number of output bytes (off-len): public void write​(byte[] b, int off, int len) throws IOException The most commonly used

But a core problem that needs to be noted is that the OutputStream class is an abstract class after all, and this abstraction must be up-cast through subclass instances if you want to obtain instantiated objects.

If you are doing file processing operations, you can use the FileOutputStream subclass;

Because the processing relationship of upward transformation needs to occur in the end, the core focus of the FileOutputSream subclass at this time can be placed on the construction method:

  • [Override] Construction method: public FileOutputStream​(File file) throws FileNotFoundException
  • 【追加】构造方法:public FileOutputStream​(File file,boolean append) throws FileNotFoundException

Example: Use the OutputSrteam class to achieve content output (English is not good, use Chinese instead of name)

package 字节流与字符流;


import java.io.*;
import java.nio.charset.StandardCharsets;

public class OutputStream字节输出流 {
    public static void main(String[] args) throws IOException {
        File file = new File("F:"+File.separator+"Test"+File.separator+"test.txt");   //1、指定了要操作文件的路径
        if(!file.getParentFile().exists()){ //文件不存在
            file.getParentFile().mkdirs();  //创建父目录
        }
        OutputStream outputStream = new FileOutputStream(file);   //2、通过子类实例化
        String str = "www.baidu.com";   //要输出的文件内容
        outputStream.write(str.getBytes()); //3、将字符串变为字节数组并输出
        outputStream.close();   //4、关闭资源
    }
}

At this time, the "www.baidu.com" content already exists in the test.text file under Test under Disk F.

This program uses the most standard form to realize the output operation processing, and in the overall processing, only the parent directory of the file is created, and the file can be automatically created by the user after execution.

Reminder: Since the OutputStream subclass also belongs to the subclass of the AutoCloseable interface, the colse() method can also be used in a simplified manner. It does not need to be opened manually, just use try... cath.

The usage of the close() method of the AutoCloseable interface is here

Example: Realize automatic closing processing

package 字节流与字符流;


import java.io.*;
import java.nio.charset.StandardCharsets;

public class OutputStream字节输出流 {
    public static void main(String[] args) throws IOException {
        File file = new File("F:"+File.separator+"Test"+File.separator+"test.txt");   //1、指定了要操作文件的路径
        if(!file.getParentFile().exists()){ //文件不存在
            file.getParentFile().mkdirs();  //创建父目录
        }
        try(OutputStream outputStream = new FileOutputStream(file)){//2、通过子类实例化
            String str = "www.baidu.com";   //要输出的文件内容
            outputStream.write(str.getBytes()); //3、将字符串变为字节数组并输出
        }catch (IOException e) {
             e.printStackTrace();
        }
    }
}

After each execution, "www.baidu.com" will be output in this file, but it will be overwritten (there is always only one). If you don’t want to be overwritten, you can use the additional construction method: public FileOutputStream​(File file,boolean append) throws FileNotFoundException

package 字节流与字符流;


import java.io.*;
import java.nio.charset.StandardCharsets;

public class OutputStream字节输出流 {
    public static void main(String[] args) throws IOException {
        File file = new File("F:"+File.separator+"Test"+File.separator+"test.txt");   //1、指定了要操作文件的路径
        if(!file.getParentFile().exists()){ //文件不存在
            file.getParentFile().mkdirs();  //创建父目录
        }
        try(OutputStream outputStream = new FileOutputStream(file,true)){//2、通过子类实例化,追加的构造方法
            String str = "www.baidu.com";   //要输出的文件内容
            outputStream.write(str.getBytes()); //3、将字符串变为字节数组并输出
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Whether to use automatic shutdown depends on the overall structure of the project. The whole program outputs a set of byte data, but don't forget that there are three output methods defined in the OutputStream class.

Guess you like

Origin blog.csdn.net/weixin_46245201/article/details/112759411