(IO operation in-depth) Memory operation flow

Memory operation flow

The characteristics of the file operation stream: the program uses InputStream to read the content of the file, and then the program uses OutputAStream to output the content to the file, all operations are based on the file (the following picture is incorrect, it is JDK1.0)

Assuming that IO operations need to be implemented now, and you do not want to generate files ( temporary files ) , you can use memory as the terminal for processing. The process at this time is as follows:

There are two types of operation flow provided in Java:

  • Byte memory operation stream: ByteArrayOutputStream, ByteArrayInputStream;

 

 

  • Character memory operation stream: CharArrayWriter, CharArrayReader;

 

 The following ByteArrayOutputStream and ByteArrayInputStraeam classes are mainly used for memory usage analysis. First, analyze their construction methods:

  • ByteArrayOutputStream construction: public ByteArrayOutputStream(); (Create a new ByteArrayOutputStream. The buffer capacity is initially 32 bytes, but its size will increase when necessary.)
  • ByteArrayInputStraeam structure: public ByteArrayInputStream​(byte[] buf); (Create a ByteArrayInputStream so that it uses buf as its buffer array.)

There is an important method in the ByteArrayOutputStream class. This method can obtain the data information stored in the memory stream. This method is

  • Get data: public byte[] toByteArray();
  • Use the form of string to obtain: public String toString();

Example: Use internal flow to implement a lowercase letter to uppercase letter operation

 

package IO深入操作;

import java.io.*;

public class 内存操作流 {
    public static void main(String[] args) throws IOException {
        String str = "abcde";
        InputStream inputStream = new ByteArrayInputStream(str.getBytes()); //将数据保存到内存流
        OutputStream outputStream = new ByteArrayOutputStream();    //读取内存中的数据
        int data = 0;
        while((data = inputStream.read()) != -1){   //每次读取一个字节
           outputStream.write( Character.toUpperCase(data));  //保存数据
        }
        System.out.println(outputStream);   //toString使用字符串获取数据
        inputStream.close();
        outputStream.close();
    }
}

 ABCDE

If you do not want to return in the form of a string, because it may store other binary data, then you can use the extended function of the ByteArrayOutputStream subclass to get all the data.

package IO深入操作;

import java.io.*;

public class 内存操作流 {
    public static void main(String[] args) throws IOException {
        String str = "abcde";
        InputStream inputStream = new ByteArrayInputStream(str.getBytes()); //将数据保存到内存流
        //必须使用子类来调用自己的扩展方法
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();    //读取内存中的数据
        int data = 0;
        while((data = inputStream.read()) != -1){   //每次读取一个字节
           outputStream.write( Character.toUpperCase(data));  //保存数据
        }
        byte[] result = outputStream.toByteArray();    //获取全部数据
        System.out.println(new String(result));
        inputStream.close();
        outputStream.close();
    }
}

ABCDE

At the beginning, ByteArrayOutputStream can be used to read large-scale text files.

Guess you like

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