What is byte stream, buffered byte stream and its cases

byte stream

1. Byte stream:

(1) Byte input stream: InputStream, this stream is an abstract class, we use the FileInputStream class to implement file input when using it (2)
Byte output stream: OutputStream, this stream is an abstract class, we use it when using The FileOutputStream class implements file output
(3) All stream operations are performed on the basis of byte streams, the original data is read through byte streams, and then handed over to other streams for processing
(4) Byte input and output streams are used Character files can be operated, but it is cumbersome and needs to be transcoded. If operating character files, it is recommended to use character streams
(5) using byte streams is more suitable for operating binary files such as pictures, audio, video, and compressed packages. Character files can also be operated but Not recommended

2. Byte stream with buffer:

(1) The byte stream with a buffer belongs to the packaging stream, and the packaging stream for the original stream
(2) Java's IO stream provides a byte input stream with a buffer (BufferedInputStream), a word with a buffer Section output stream (BufferedOutputStream)
(3) Since the stream with a buffer is a wrapper stream for the original stream, the stream with a buffer must be used on the basis of the original stream (
4) The buffer stream is thread-safe Yes, the accuracy of the data can be guaranteed in the concurrent situation, and the synchronized synchronization lock is used in the buffer stream to achieve

the case

1. Byte output stream

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Scanner;

public class 字节输出流 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        Scanner scanner = new Scanner(System.in);
        /**
         * 创建字节输出流对象,并指定输出的目标文件
         * 如果目标文件不存在则自动创建该文件,如果存在则替换该文件
         * 当第二个参数设置为true时,表示向文件中追加内容(如果文件不存在依然会创建新文件)
         */

        OutputStream outputStream = new FileOutputStream("f:/abcx.txt",true);
        //循环向文件输出内容
        while(true){
    
    
            System.out.print("请输入内容:");
            String str = scanner.next();
            if(str.equals("exit")){
    
    
                break;//退出
            }
            //将字符串转换为字节数组
            byte[] buf = str.getBytes();
            outputStream.write(buf);
            //刷新流
            outputStream.flush();
        }

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

2. Byte input stream

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * 字节输入流实例-将磁盘上文件中的数据读入到程序中
 */
public class 字节输入流 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        /**
         *  创建输入流对象,并指定要读取的文件
         */

        InputStream inputStream = new FileInputStream("f:/mysql5.7解压缩安装.txt");
        //读取一个字节,该字节为一个整数
        /*int num = inputStream.read();*/
        //声明一个用于存储读取字节的缓冲区(字节数组)
        byte[] buf = new byte[1024];
        /**
         * 读取数据,读取缓冲区指定大小的数据
         * 返回实际读取字节的数量(长度),如果未读取到数据则返回-1
         */
        int len = - 1;
        //循环读取文件中的数据,当len的值为-1时表示读取结束
        while((len=inputStream.read(buf,0,buf.length))!=-1){
    
    
            /**
             * 将字节数组中的字节转换字符串
             * 指定转换的起始下标,及转换长度
             */

            String str = new String(buf,0,len);

            System.out.println(str);
        }


        inputStream.close();//关闭输入流

    }
}

3. File Copy

import java.io.*;

public class 文件复制 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //创建文件输入流对象,并指定源文件
        InputStream inputStream = new FileInputStream("e:/maven-lib.rar");
        //创建文件输出流对象,并指定目标文件
        OutputStream outputStream = new FileOutputStream("f:/maven-lib.rar");
        /**
         * 创建字节数组做为缓冲区
         * 可以通过设置缓冲区的大小,提高效率,设置缓冲区时根据实际情况设置,不是越大越好
         */

        byte[] buf = new byte[1024*1024*5];
        //表示输入流读取的字节数
        int len = -1;
        //获得开始复制的毫秒数
        long start = System.currentTimeMillis();
        //使用while循环读取文件中的数据并写出到目标文件
        while((len=inputStream.read(buf,0,buf.length)) != -1){
    
    
            //将读到的数据写出到磁盘指定的文件中
            outputStream.write(buf,0,len);
            //刷新输出流
            outputStream.flush();
        }

        //获得复制复制结束时的毫秒数
        long end = System.currentTimeMillis();
        System.out.println("耗时:"+(end-start)+"毫秒");
        //关闭流
        inputStream.close();
        outputStream.close();
    }
}

4. File copy_Using a byte stream with a buffer to achieve

import java.io.*;

public class 文件复制_使用带有缓冲区的字节流实现 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //创建文件输入流对象,并指定源文件
        InputStream inputStream = new FileInputStream("e:/maven-lib.rar");
        //创建文件输出流对象,并指定目标文件
        OutputStream outputStream = new FileOutputStream("f:/maven-lib.rar");
        //使用BufferedInputStream和BufferedOutputStream对原始的字节输入输出流进行包装
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream,1024*1024*5);
        //创建带有缓冲区的字节输出流
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
        /**
         * 创建字节数组做为缓冲区
         * 可以通过设置缓冲区的大小,提高效率,设置缓冲区时根据实际情况设置,不是越大越好
         */

        byte[] buf = new byte[1024];
        //表示输入流读取的字节数
        int len = -1;
        //获得开始复制的毫秒数
        long start = System.currentTimeMillis();
        //使用while循环读取文件中的数据并写出到目标文件
        while((len=bufferedInputStream.read(buf,0,buf.length)) != -1){
    
    
            //将读到的数据写出到磁盘指定的文件中
            bufferedOutputStream.write(buf,0,len);
            //刷新输出流
            bufferedOutputStream.flush();
        }

        //获得复制复制结束时的毫秒数
        long end = System.currentTimeMillis();
        System.out.println("耗时:"+(end-start)+"毫秒");
        //关闭流
        inputStream.close();
        outputStream.close();
        bufferedInputStream.close();
        bufferedOutputStream.close();
    }
}

Guess you like

Origin blog.csdn.net/weixin_52682014/article/details/127644377